firebase 在列表视图生成器上找不到正确的提供程序

jvlzgdj9  于 2023-05-01  发布在  其他
关注(0)|答案(1)|浏览(117)

我试图创建一个列表视图生成器,有列表平铺显示用户从Firebase的帖子,但我有这个问题:

The following ProviderNotFoundException was thrown building Profile(dirty, state: _ProfileState#4f043):
Error: Could not find the correct Provider<List<PostModel>> above this Profile Widget

This happens because you used a `BuildContext` that does not include the provider
of your choice. There are a few common scenarios:

这是我的代码:

imports...

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

  @override
  State<Profile> createState() => _ProfileState();
}

class _ProfileState extends State<Profile> {
  String? picProfile = '';
  String? picPost = '';
  final AuthService _authService = AuthService();
  final PostService _postService = PostService();
  @override
  Widget build(BuildContext context) {
    final ap =  Provider.of<AuthProvider>(context, listen: false);
    final posts = Provider.of<List<PostModel>>(context) ?? [];
    picProfile = ap.userModel.profilePic;
    return Scaffold(
    appBar: AppBar(
      backgroundColor: Colors.black,
      title: Text("${ap.userModel.usuario}", style: TextStyle(color: Colors.white, fontSize: 24),),
    actions: <Widget>[
      TextButton(
          child: Text('Sair', style: TextStyle(color: Colors.white, fontSize: 16),),
          onPressed: () async {ap.userSignOut().then((value) => Get.offAll(WelcomeScreen()));
          })
    ],
    ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            CircleAvatar(
              backgroundColor: Colors.purple,
              backgroundImage: NetworkImage(ap.userModel.profilePic!),
              radius: 50,
            ),
            const SizedBox(height: 20,),
          Text(ap.userModel.fullName!, style: TextStyle(color: Colors.black),),
          const SizedBox(height: 5,),
          Text("@" + ap.userModel.usuario!, style: TextStyle(color: Colors.black),),
            const SizedBox(height: 5,),
            Text("Bio: " + ap.userModel.bio!, style: TextStyle(color: Colors.black),),
            const SizedBox(height: 15,),
            StreamProvider.value(value: _postService.getPostsByUser(FirebaseAuth.instance.currentUser!.uid), initialData: [],
              child: SingleChildScrollView(
                child: ListView.builder(
                  itemCount: posts.length,
                  itemBuilder: (_, index){
                    final post = posts[index];
                    return ListTile(
                      leading: CircleAvatar(backgroundImage: picProfile != '' ? NetworkImage(picProfile!) : Image.asset('assets/image/p1.PNG')as ImageProvider<Object>),
                      title: Column(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Padding(
                            padding: const EdgeInsets.all(0.0),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Expanded(
                                    child: Padding(
                                      padding: const EdgeInsets.all(8.0),
                                      child: Wrap(
                                        spacing: 5.0,
                                        runSpacing: 5.0,
                                        direction: Axis.horizontal,
                                        children: [
                                          Text(ap.userModel.fullName!, style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold,color: Colors.black),),
                                          Text('@${ap.userModel.usuario}',style: TextStyle(fontSize: 13, fontStyle: FontStyle.normal, color: Colors.grey),),
                                          Text('um minuto atrás', style: TextStyle(fontSize: 13, fontStyle: FontStyle.normal, color: Colors.grey),)
                                        ],
                                      ),
                                    ))
                              ],
                            ),
                          ),
                        ],
                      ),
                      subtitle: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          SizedBox(
                            height: 10,
                          ),
                          Text(post.text! ,overflow: TextOverflow.clip, maxLines: 4, style: TextStyle(color: Colors.black),),
                          SizedBox(
                            height: 10,
                          ),
                          picPost != '' ?
                          Image.network(picPost!) : SizedBox(height: 1,) ,
                          SizedBox(height: 10,),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Icon(Icons.favorite_border, color: Colors.grey,),
                              Icon(Icons.chat_bubble_outline, color: Colors.grey,),
                              Icon(Icons.share, color: Colors.grey,)
                            ],
                          )

                        ],
                      ),
                    );
                  },
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}

我怎样才能解决这个问题?我问自己,如果问题是,我使用一个以上的供应商,如ap和职位,但idk如何解决它。任何帮助都会很棒〈3
我的帖子模型:

import 'package:cloud_firestore/cloud_firestore.dart';

class PostModel {
  final String? id;
  final String? creator;
  final String? picPost;
  final String? text;
  final Timestamp? timestamp;

  PostModel({this.id, this.creator, this.text, this.timestamp, this.picPost});

}
r9f1avp5

r9f1avp51#

我认为问题是在这里调用提供者时传递了模型。

Provider.of<List<**PostModel**>>(context) ?? [];

相关问题