【问题标题】:how to retrieve phone number from firebase using flutter如何使用颤振从firebase中检索电话号码
【发布时间】:2020-10-20 13:25:59
【问题描述】:

如何在 Firebase 中检索电话身份验证期间使用的电话号码并使用颤振显示它,我尝试了以下方法,但它不适用于我:

    FirebaseAuth.instance.currentUser().then((user) {
      _userId = user.uid;
      _phone = user.phoneNumber;
    });

这是我的完整代码

    class HomePageState extends State<HomeScreen>{
  String _userId,_phone;

  @override
  Widget build(BuildContext context) {
       FirebaseAuth.instance.currentUser().then((user) {
      _userId = user.uid;
      _phone = user.phoneNumber;
    });

    var myGridView = new GridView.builder(
      itemCount: services.length,
      gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 3,
        // childAspectRatio: (itemWidth / itemHeight),
      ),
      itemBuilder: (BuildContext context, int index) {
        return FlatButton(
          child: Padding(
            padding: const EdgeInsets.all(0.0),
            // child:new Card(
            //elevation: 5.0,
            child: new Container(
              alignment: Alignment.centerLeft,
              child: Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    new Text(_userId), 
                  ],
                ),
              ),
            ),
          ),
          onPressed: () {
            String catgry = services[index];
            if (catgry == "coming soon") {
              showDialog(
                  barrierDismissible: false,
                  context: context,
                  child: new CupertinoAlertDialog(
                    title: new Column(
                      children: <Widget>[
                        new Text("This feature is coming soon"),
                        new Icon(
                          Icons.favorite,
                          color: Colors.red,
                        ),
                      ],
                    ),
                    // content: new Text( services[index]),
                    actions: <Widget>[
                      new FlatButton(
                          onPressed: () {
                            Navigator.of(context).pop();
                          },
                          child: new Text("OK"))
                    ],
                  ));
            } else {
              Navigator.push(
                  context,
                  new MaterialPageRoute(
                      builder: (context) => Details(catgry,phone1)));
            }
          },
        );
      },
    );

    return new Scaffold(
      appBar: new AppBar(
        backgroundColor: Colors.black,
        title: Text("Appbar", style: TextStyle(color: Colors.white),),
      ),),
      body: myGridView,
      bottomNavigationBar: CustomBottomBar(),
    );
  }
}

【问题讨论】:

  • “它不工作”是什么意思。更多细节会有所帮助
  • 当我使用它时,它给了我这个错误“引发了另一个异常:必须向文本小部件提供非空字符串。”
  • 发布完整代码,包括您的 Text 小部件的代码
  • 哦,我发了,请检查

标签: firebase flutter firebase-authentication flutter-layout flutter-dependencies


【解决方案1】:

因为您使用的是 StatefulWidget,您可以创建一个 getUser 函数,该函数将更新 userId 和电话,然后在您的 initState 中调用该函数。获取值时还添加带有Center(child: CircularProgressIndicator()) 的加载屏幕

class _HomeScreenState extends State<HomeScreen> {
  String _userId,_phone;
  bool loading = true;
  
  getUser(){
    FirebaseAuth.instance.currentUser().then((user) {
      _userId = user.uid;
      _phone = user.phoneNumber;
      loading = false;
      setState(() {});
    });
  }

  @override
  void initState() {
    super.initState();
    getUser();
  }
  
  @override
  Widget build(BuildContext context) {
    var myGridView = new GridView.builder(
      itemCount: services.length,
      gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 3,
        // childAspectRatio: (itemWidth / itemHeight),
      ),
      itemBuilder: (BuildContext context, int index) {
        return FlatButton(
          child: Padding(
            padding: const EdgeInsets.all(0.0),
            // child:new Card(
            //elevation: 5.0,
            child: new Container(
              alignment: Alignment.centerLeft,
              child: Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    new Text(_userId),
                  ],
                ),
              ),
            ),
          ),
          onPressed: () {
            String catgry = services[index];
            if (catgry == "coming soon") {
              showDialog(
                  barrierDismissible: false,
                  context: context,
                  child: new CupertinoAlertDialog(
                    title: new Column(
                      children: <Widget>[
                        new Text("This feature is coming soon"),
                        new Icon(
                          Icons.favorite,
                          color: Colors.red,
                        ),
                      ],
                    ),
                    // content: new Text( services[index]),
                    actions: <Widget>[
                      new FlatButton(
                          onPressed: () {
                            Navigator.of(context).pop();
                          },
                          child: new Text("OK"))
                    ],
                  ));
            } else {
              Navigator.push(
                  context,
                  new MaterialPageRoute(
                      builder: (context) => Details(catgry,phone1)));
            }
          },
        );
      },
    );

    return new Scaffold(
      appBar: new AppBar(
        backgroundColor: Colors.black,
        title: Text("Appbar", style: TextStyle(color: Colors.white),),
      ),
      body: loading?Center(child: CircularProgressIndicator()):myGridView,
      bottomNavigationBar: CustomBottomBar(),
    );
  }
}

【讨论】:

  • 是的,但我的声望低于 15,所以它显示为记录但未公开显示。
【解决方案2】:

您可以使用 async/await 来执行此操作,一旦获得数据,您就可以调用 setState()

String phone;

@override
void initState(){
  super.initState();
  getPhone();
}

getPhone() async{
 FirebaseUser currentUser = await FirebaseAuth.instance.currentUser();
 setState(() {
   phone=currentUser.phoneNumber;
 });
}


【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    • 2020-09-10
    • 2020-12-03
    • 2022-08-23
    • 2011-05-14
    相关资源
    最近更新 更多