0

我试图从 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'

这是我的东西。飞镖

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)
              ),
        ));
  }
}

任何事情都会有帮助!

4

2 回答 2

0

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

flutter pub add firebase_core

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

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

于 2021-12-26T02:19:41.663 回答
0

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

所以,我删除了上面几乎所有的文件。而不是我只使用该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(),
          );
        });
  }
}

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

于 2021-12-27T12:19:10.200 回答