提供程序在JSON列表上显示null

问题描述

在我的flutter应用程序中,我正在使用提供程序体系结构从API提取JSON,并在整个应用程序的小部件上显示。但是,我在返回的列表上获取空值。该API发送回响应,并根据此值正确映射和存储该响应:

  StreamController<List<User>> _allUsersController =
      StreamController<List<User>>();
  Stream<List<User>> get allusers => _allUsersController.stream;

  Future<bool> getAllUsers() async {
    var hasValue = false;
    var bookings = await api.getAllUsers();
    if (bookings != null) {
      _allUsersController.add(users);
      hasValue = true;
    }
    return hasValue;
  }

按照filledstacks的体系结构,然后像这样将其添加到uiConsumableProviders中:

List<SingleChildWidget> uiConsumableProviders = [
  StreamProvider<List<User>>(
    create: (context) =>
        Provider.of<UserService>(context,listen: false).allusers,),];

但是,当我尝试访问文本小部件中返回的模型列表时,它说vaue在null上调用。我知道我做错了什么,但我无法弄清楚是什么。

Text(
   'Welcome ${Provider.of<List<User>>(context)[0].name}',style: GoogleFonts.lato(
      fontSize: 20,color: Colors.white,fontWeight: FontWeight.bold)),

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)