Flutter ChannelListView显示频道列表,但在列表末尾显示错误
我正在尝试使用getstream.io中的flatter和stream chat flatter构建应用程序 除发现2个问题外,所有操作均按预期运行Flutter ChannelListView显示频道列表,但在列表末尾显示错误,flutter,getstream-io,Flutter,Getstream Io,我正在尝试使用getstream.io中的flatter和stream chat flatter构建应用程序 除发现2个问题外,所有操作均按预期运行 当频道列表为空(在我删除所有频道后)并且我尝试创建新频道时,ChannelListView不会更新,我必须重新初始化小部件以使其重新工作 当频道列表显示某个频道时,会显示错误文本“加载频道时出错” 我在VSC上看到了日志并跟踪了链接,它显示QueryChannel失败,错误代码为401代码2(auth error)。我检查了一切,包括我的PHP
void main() {
WidgetsFlutterBinding.ensureInitialized();
setupLocator();
// Enable integration testing with the Flutter Driver extension.
// See https://flutter.io/testing/ for more info.
runApp(new MyApp());
// Register to receive BackgroundFetch events after app is terminated.
// Requires {stopOnTerminate: false, enableHeadless: true}
BackgroundFetch.registerHeadlessTask(backgroundFetchHeadlessTask);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Test',
theme: ThemeData(
// primaryColor: Color.fromRGBO(5,37,17,1),
primaryColor: Color.fromRGBO(255, 255, 255, 1),
primaryTextTheme:
TextTheme(headline6: TextStyle(color: Colors.black54)),
inputDecorationTheme: InputDecorationTheme(
labelStyle: TextStyle(
color: Colors.black54,
// fontSize: 24.0
),
),
),
builder: (context, child) {
return scf.StreamChat(
child: scf.ChannelsBloc(child: child),
// child: child,
client: locator<ChatService>().client,
);
},
home: Scaffold(resizeToAvoidBottomInset: true, body: LoginScreen()));
}
}
截图
getCredential() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
final JsonDecoder _decoder = new JsonDecoder();
final postUrl =
"http://<link-to-my-local>/sc-get-token/${prefs.getString("userId")}";
final response = await http.post(postUrl);
var data = _decoder.convert(response.body);
if (prefs.getString("userId") != null) {
final client = locator<ChatService>().client;
await client.disconnect();
await client.connectUser(
User(
id: prefs.getString("userId"),
extraData: {
'name': StringUtils.capitalize(prefs.getString("userName")),
},
),
data['token'],
);
setState(() {
Navigator.pushReplacement(
context,
CupertinoPageRoute(builder: (context) => new ListChannel()),
);
});
}
}
class ListChannel extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
backgroundColor: Color.fromRGBO(5, 37, 17, 1),
onPressed: () async {
await Navigator.push(
context,
new CupertinoPageRoute(
builder: (BuildContext context) => new NewChannel()));
},
),
appBar: new AppBar(title: Text("List Channel Diskusi")),
body: ChannelListView(
// emptyBuilder: (context) {
// return Column(
// mainAxisAlignment: MainAxisAlignment.center,
// children: [
// Center(
// child: Icon(
// Icons.forum,
// size: 56.0,
// color: Colors.grey,
// ),
// ),
// Padding(padding: EdgeInsets.only(top: 15.0)),
// Center(child: Text("No channels found"))
// ],
// );
// },
filter: {
// "type": "messaging",
'members': {
'\$in': [StreamChat.of(context).user.id],
}
},
sort: [
SortOption('last_message_at')
],
// pagination: PaginationParams(limit: 20),
channelPreviewBuilder: (context, channel) =>
_channelPreviewBuilder(context, channel)),
);
}
Widget _channelPreviewBuilder(BuildContext context, Channel channel) {
var lastMessage, subtitle, lastText, opacity;
var lastTime = "";
subtitle = channel.extraData['step'];
if (channel.state.messages.length > 0) {
lastMessage = channel.state.messages.reversed
.firstWhere((message) => !message.isDeleted);
lastText = "${lastMessage.user.name}: ${lastMessage.text}";
var lastTimeMSE, date, now, today, yesterday, aDate;
if (lastMessage.updatedAt != null) {
lastTimeMSE = lastMessage.updatedAt.millisecondsSinceEpoch;
date = DateTime.fromMillisecondsSinceEpoch(lastTimeMSE);
now = DateTime.now();
today = DateTime(now.year, now.month, now.day);
yesterday = DateTime(now.year, now.month, now.day - 1);
aDate = DateTime(date.year, date.month, date.day);
if (aDate == today) {
lastTime = DateFormat.Hm().format(date).toString();
} else if (aDate == yesterday) {
lastTime = "Yesterday";
} else {
lastTime = DateFormat('dd/MM/yy').format(date).toString();
}
}
opacity = channel.state.unreadCount > .0 ? 1.0 : 0.5;
} else {
lastText = "";
lastTime = "";
opacity = 0.5;
}
return ListTile(
contentPadding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 15.0),
// leading: ChannelImage(
// channel: channel,
// ),
leading: IconButton(
icon: Icon(Icons.delete),
onPressed: () async {
await channel.delete();
}),
title: ChannelName(
textStyle:
StreamChatTheme.of(context).channelPreviewTheme.title.copyWith(
color: Colors.black.withOpacity(opacity),
),
),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: EdgeInsets.symmetric(vertical: 4.0),
child: Text(
subtitle,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
Text(
lastText,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
],
),
trailing: Container(
width: 65.0,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Padding(
padding: EdgeInsets.only(bottom: 5.0),
child: Text(lastTime, style: TextStyle(fontSize: 12.0))),
channel.state.unreadCount > 0
? CircleAvatar(
radius: 7,
backgroundColor: Colors.green,
)
: Icon(Icons.check, size: 15.0),
],
),
),
onTap: () async {
await Navigator.of(context).push(
CupertinoPageRoute(
builder: (context) => StreamChannel(
child: ChannelPage(channel: channel),
channel: channel,
),
),
);
},
);
}
}