【问题标题】:An exception was throw by _MapStream<QuerySnapshot<Map<String, dynamic>>, List<Stuff>> listened by_MapStream<QuerySnapshot<Map<String, dynamic>>, List<Stuff>> 抛出异常
【发布时间】:2022-01-25 15:42:47
【问题描述】:

我试图从 Firestore 数据库中使用 ListTile 列出一些数据。在从数据库中提取数据时,我得到了下面的异常。数据库中所有属性的名称和代码都相同。我不知道它为什么会抛出它。

The following assertion was thrown:
An exception was throw by _MapStream<QuerySnapshot<Map<String, dynamic>>, List<Stuff>> listened by

StreamProvider<List<Stuff>?>, but no `catchError` was provided.

Exception:
type 'int' is not a subtype of type 'String'

这是我的 stuff.dart

class Stuff {
  final String title;
  final String details;
  final String price;

  Stuff({
    required this.title,
    required this.details,
    required this.price,
  });
}

这是我的 database.dart

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:unistuff_main/models/stuff.dart';

class DatabaseService {
  final String? uid;
  DatabaseService({this.uid});

  //collection reference
  final CollectionReference stuffCollection =
      FirebaseFirestore.instance.collection('stuffs');

//stufflist from snapshot
  List<Stuff> _stuffListFromSnapshot(QuerySnapshot snapshot) {
    return snapshot.docs.map((doc) {
      return Stuff(
        title: doc.get('title') ?? '0',
        price: doc.get('price') ?? '0',
        details: doc.get('details') ?? '0',
      );
    }).toList();
  }

//get the stuffs
  Stream<List<Stuff>> get stuffs {
    return stuffCollection.snapshots().map(_stuffListFromSnapshot);
  }
}

这是我的 stuff_list.dart

import 'package:flutter/material.dart';
import 'package:unistuff_main/models/stuff.dart';
import 'package:provider/provider.dart';
import 'package:unistuff_main/screens/home/stuff_tile.dart';

class StuffList extends StatefulWidget {
  const StuffList({Key? key}) : super(key: key);

  @override
  _StuffListState createState() => _StuffListState();
}

class _StuffListState extends State<StuffList> {
  @override
  Widget build(BuildContext context) {
    final stuffs = Provider.of<List<Stuff>?>(context);
    //print(stuffs?.docs);
    if (stuffs != null) {
      stuffs.forEach((stuff) {
        print(stuff.title);
        print(stuff.details);
        print(stuff.price);
      });
    }
    return ListView.builder(
      //number of the items in list
      itemCount: stuffs?.length ?? 0,
      //return a function for every item in the list
      itemBuilder: (context, index) {
        return StuffTile(stuff: stuffs![index]);
      },
    );
  }
}

stuff_tile.dart

import 'package:flutter/material.dart';
import 'package:unistuff_main/models/stuff.dart';

class StuffTile extends StatelessWidget {
  //const StuffGrid({ Key? key }) : super(key: key);

  //sending the data to stuff
  final Stuff? stuff;
  StuffTile({this.stuff});

  @override
  Widget build(BuildContext context) {
    //creating the list view
    return Padding(
        padding: EdgeInsets.only(top: 8.0),
        child: Card(
          margin: EdgeInsets.fromLTRB(20.0, 6.0, 20.0, 0.0),
          child: ListTile(
              leading: CircleAvatar(
                radius: 25.0,
                backgroundColor: Colors.brown,
              ),
              title: Text(stuff!.title),
              subtitle: Text(stuff!.details)
              ),
        ));
  }
}

一切都会有帮助!

【问题讨论】:

    标签: flutter dart google-cloud-firestore listtile


    【解决方案1】:

    您可能缺少 firebase_core 的依赖项,这是我在将文件添加到新的空白项目时遇到的第一个问题。您可以在终端中运行它:

    flutter pub add firebase_core
    

    我也没有看到针对您给出的错误列出的代码 ("_MapStream>, List>")。该项目对我来说运行良好。

    您可以在Github 上查看我的项目代码。

    【讨论】:

    • 嗨,我的 firebase_core 是最新的,并且在依赖项中有一个依赖项。不幸的是它没有解决我的问题:(我认为它对你没有问题,因为你的应用没有连接到我的数据库,所以它只是通过了那些部分。
    【解决方案2】:

    我搜索了很多并看到了不同的方法,但我看不到如何将它们实现到我自己的代码中。因此,我采用了一种不同的更基本的方法,您可以从here 阅读它。

    所以,我删除了上面几乎所有的文件。而不是我只使用stuff_list.dart 文件作为我的主要列表文件。然后,我从 home.dart 文件中调用它。因此,目前一切正常。

    import 'package:cloud_firestore/cloud_firestore.dart';
    import "package:flutter/material.dart";
    
    class StuffList extends StatelessWidget {
      const StuffList({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: _stuffList(),
        );
      }
    }
    
    class _stuffList extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        final Stream<QuerySnapshot> _usersStream =
            FirebaseFirestore.instance.collection('Stuffs').snapshots();
    
        return StreamBuilder<QuerySnapshot>(
            stream: _usersStream,
            builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
              if (snapshot.hasError) {
                return Text('Something went wrong');
              }
    
              if (snapshot.connectionState == ConnectionState.waiting) {
                return Text("Loading");
              }
    
              return ListView(
                children: snapshot.data!.docs.map((DocumentSnapshot document) {
                  Map<String, dynamic> data =
                      document.data()! as Map<String, dynamic>;
                  return ListTile(
                    title: Text(data['title']),
                    subtitle: Text(data['details']),
                  );
                }).toList(),
              );
            });
      }
    }
    

    但我不会接受我的答案作为答案,因为这与我的问题无关。因为它不能解决我上面遇到的问题。它只是绕过它。

    【讨论】:

      猜你喜欢
      • 2018-12-01
      • 1970-01-01
      • 2021-04-13
      • 2021-08-13
      • 2021-03-25
      • 1970-01-01
      • 2020-11-05
      • 1970-01-01
      • 2020-12-09
      相关资源
      最近更新 更多