Flutter:FirebaseException([cloud_firestore/unavailable]该服务当前不可用,如何修复它?

im9ewurl  于 2023-02-13  发布在  Flutter
关注(0)|答案(2)|浏览(150)

在我创建个人资料页面的过程中,我遇到了许多障碍,这是最新的一个
当我从firestore调用用户名时会发生这种情况
要了解更多问题,请查看我前面的问题。

class profile extends StatefulWidget {
  const profile({Key? key,required this.userProfile}) : super(key: key);
  final String? userProfile;

  
  

  @override
  State<profile> createState() => _profileState();
}

class _profileState extends State<profile> {
  final userRef = FirebaseFirestore.instance.collection('users');

  Future<UserModel?> getData() async {
    final userRef = FirebaseFirestore.instance.collection('users');
    final doc = await userRef.doc(widget.userProfile).get();
    if (doc.exists) {
      var data = doc.data();
    } else {
      return null;
    }
    final data = await FirebaseFirestore.instance
        .collection('users')
        .doc(userRef.id)
        .get();

    if (data.exists) {
      UserModel user = UserModel.fromMap(data.data()!);
      return user;
    }
  }

    late final future = getData();

   Widget buildprofileheader() {
   
    return FutureBuilder<UserModel?>(future:future,
    builder: ((context, snapshot) {
      if(!snapshot.hasData){
      UserModel user=UserModel.fromMap(userRef.parameters);
      return Padding(padding:EdgeInsets.all(16.0),
      child: Column(
        children: [
                  Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Icon(Icons.account_circle, size: 90,)
            ],
          ),
          Container(
            alignment: Alignment.center,
            padding: EdgeInsets.all(12.0),
            child: Text(
              user.Username ?? "defualtVlueNullCase",
              style: TextStyle(
                fontWeight: FontWeight.bold,
                fontSize:17.0,
              ),
            ),
          ),
        ],
      ),
      );}
      else{
      return CircularProgress();}
    }),
       );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          centerTitle: true,
          title: Text(
            "Profile",
          ),
        ),
        body: ListView(children: <Widget>[
           buildprofileheader(),
        ]));
  }
}

当我点击它显示我下面的图片之后,vscode显示此页面error_patch.dart指示此行出于某些原因

// external static Never _throw(Object error, StackTrace stackTrace);

0h4hbjxa

0h4hbjxa1#

这是因为您尚未在firebase firestore console中创建集合和文档。

oymdgrw7

oymdgrw72#

我确实通过添加来修复它

@override
  void initState() {
    super.initState();
    FirebaseFirestore.instance
     .collection('users')
     .doc(user!.uid)
     .get()
     .then((value){
      this.thisUser=UserModel.fromMap(value.data());
      setState(() {});
     });
  ```!

also change from this :
```dart
 child: Text(
              user.Username ?? "defualtVlueNullCase",
              style: TextStyle(
                fontWeight: FontWeight.bold,
                fontSize:17.0,
```!
to this:
```dart
child:Text(
             "${thisUser.username}",
              style: TextStyle(
                fontWeight: FontWeight.bold,
                fontSize:17.0,
```!

相关问题