Flutter 错误:期望类型为“String”的值,但得到类型为“List<dynamic>”的值

时间:2021-05-28 13:07:06

标签: flutter dart

我是堆栈溢出的新手,这是我的第一个问题。抱歉,如果我问错了。我正在尝试从 firebase 流式传输数据以显示在 DataTable 中,但出现错误。将不胜感激。

下面是我的代码:

Container(
 child: ListView(
   shrinkWrap: true,
   padding: const EdgeInsets.all(16),
   children: [
      Container(
         color: kDarkBlackColor,
         child: new StreamBuilder<QuerySnapshot>(
            stream: _movieStream,
            builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
               if (!snapshot.hasData || snapshot.data.docs == null)
               {
                  print("no Data");
                  return LinearProgressIndicator();
               }
               else if (snapshot.data.docs.length > 0) {
                  print("Has Data");
                  return DataTable(
               columns: [
                  DataColumn(
                     label: Text('Prev',
                        style: TextStyle(
                           fontSize: 18,
                           color: kBgColor,
                           fontWeight:
                           FontWeight.bold)
                       ),
                    ),
                    DataColumn(
                       label: Text('No.',
                          style: TextStyle(
                             fontSize: 18,
                             color: kBgColor,
                             fontWeight:
                             FontWeight.bold))),
                    DataColumn(
                       label: Text('Name',
                          style: TextStyle(
                             fontSize: 18,
                             color: kBgColor,
                             fontWeight:
                             FontWeight.bold))),
                     DataColumn(
                        label: Text('Genre',
                           style: TextStyle(
                              fontSize: 18,
                              color: kBgColor,
                              fontWeight:
                              FontWeight.bold))),
                  ],
                  rows:
                     _buildList(context, snapshot.data.docs),
                  );
              }
           },
        ),
     ),
  ],

 ),
),

List<DataRow> _buildList(
    BuildContext context, List<DocumentSnapshot> snapshot) {
      return snapshot.map((data) => _buildListItem(context, data)).toList();
  }

DataRow _buildListItem(BuildContext context, DocumentSnapshot data) {
  final record = Record.fromSnapshot(data);
print("last");
  return DataRow(cells: [
    DataCell(
        Container(width: 80, child: Image.asset('assets/images/movie1.png'))),
    DataCell(Text(record.movieName)),
    DataCell(Text(record.movieRating)),
    DataCell(Text(record.movieGenre)),
  ]);
}

class Record {
  final String movieName;
  final String movieGenre;
  final String movieRating;
  final DocumentReference reference;

  Record.fromMap(Map<String, dynamic> map, {this.reference})
      : assert(map['movieName'] != null),
        assert(map['movieGenre'] != null),
        assert(map['movieRating'] != null),
        movieName = map['movieName'],
        movieGenre = map['movieGenre'],
        movieRating = map['movieRating'];

  Record.fromSnapshot(DocumentSnapshot snapshot)
      : this.fromMap(snapshot.data(), reference: snapshot.reference);

  @override
  String toString() => "Record<$movieName:$movieRating:$movieGenre>";
}

在我运行我的代码后,我得到“期望一个'String'类型的值,但得到一个'List'类型的值”。 我认为代码在“return snapshot.map((data) => _buildListItem(context, data)).toList();”

0 个答案:

没有答案