NoSuchMethodError:无效成员,null:'toList'(Flutter Web)_MapStream <QuerySnapshot引发异常

时间:2020-09-29 04:30:37

标签: flutter listview web provider

错误: NoSuchMethodError:无效成员为null:'toList 出现,但是当我使用带注释的提供程序时代码有效,即最终用户详细信息= Provider.of (context).toList()? [];

但是,如果我使用 final userdetails = Provider.of <列表>(上下文).toList()? []; 我收到这两个错误,应用显示红色屏幕。 1- NoSuchMethodError:null无效成员:'toList 2-_MapStream 侦听

引发异常
import 'package:traveltogether_admin/Admin_Screens/Admin_ManageUsers_Tile.dart';
import 'package:traveltogether_admin/Models/user.dart';

class ManageUsers extends StatelessWidget {
  static const id = 'Admin_manage_users';
  @override
  Widget build(BuildContext context) {
    final userdetails = Provider.of<List<MyUserData>>(context).toList() ?? [];
    // final userdetails = Provider.of<List<RequestToJoinData>>(context).toList() ?? [];
    final user= userdetails;
    return ListView.builder(
        // shrinkWrap: true,
        // physics: NeverScrollableScrollPhysics(),
        // primary: false,
        itemCount: userdetails.length,
        itemBuilder: (context,index)
    {
      print(userdetails);
          return ManageUserTile(user[index]);
    });
  }
}

//MyUserData is the model class.
// Rest of the code.
List<MyUserData> _MyUserDataListfromSnapshot(QuerySnapshot snapshot)
{
  return snapshot.documents.map((doc) {
    return MyUserData(
      uid: uid ?? '',
      fullname: doc.data['fullname'] ?? '',
      CNIC: doc.data['CNIC'] ?? '',
      studentID: doc.data['studentID'] ?? '',
      department: doc.data['department'] ?? '',
      phonenumber: doc.data['phonenumber'] ?? '',
      vehicle: doc.data['vehicletype'] ?? '',
      VehicleCC: doc.data['vehicleCC'] ?? '',
      carModel: doc.data['carModel'] ?? '',
      numberplate: doc.data['numberplate'] ?? '',
    );
  }).toList();
}

Stream<List<MyUserData>> get myUserDataListStream{
    return myUserCollection.snapshots().map(_MyUserDataListfromSnapshot);
}

1 个答案:

答案 0 :(得分:0)

您需要稍微改变一下方法。

final List<MyUserData> users = Provider.of<List<MyUserData>>(context);

在ListviewBuilder中执行空检查。

ListView.builder(
        // shrinkWrap: true,
        // physics: NeverScrollableScrollPhysics(),
        // primary: false,
        itemCount: userdetails?.length ?? 0,
        itemBuilder: (context,index){
     
          return ManageUserTile(users[index]);
    });