【问题标题】:Flutter app not able to skip introduction screen for old usersFlutter 应用程序无法跳过老用户的介绍屏幕
【发布时间】:2020-11-05 09:24:08
【问题描述】:

我是新来的颤振。我正在制作一个应用程序,它在第一次使用该应用程序时将电话号码作为第一个屏幕中的输入。从下一次开始,我希望应用程序跳过第一个屏幕并直接转到下一个屏幕。但我无法实现此功能。请帮助。谢谢。

import 'package:shared_preferences/shared_preferences.dart';

class PrefShare {
  String mob;
  bool isNew;

  Future<String> spNumberSetter(String no) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setString('phoneNumber', no);
  }

  Future<String> spNumberGetter() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    mob = prefs.getString('phoneNumber');
  }

  Future<bool> spAppNew() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setBool('isLoggedIn', true);
  }

  Future<bool> spAppGetter() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    isNew = prefs.getBool('isLoggedIn');
  }
}


//main
import 'package:emergency_messaging/button_screen.dart';
import 'package:flutter/material.dart';
import 'prefs.dart';

PrefShare prefShare = PrefShare();

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  prefShare.spAppGetter();
  bool status = prefShare.isNew ?? false;
  prefShare.spAppNew();
  runApp(
    MaterialApp(home: status == true ? ButtonScreen() : Home()),
  );
}

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {
    String phoneNo;
    return SafeArea(
      child: Scaffold(
        backgroundColor: Color(0xff9ad3bc),
        floatingActionButton: FloatingActionButton(
          backgroundColor: Colors.black,
          child: Icon(
            Icons.arrow_forward,
            color: Color(0xff9ad3bc),
          ),
          onPressed: () {
            showDialog(
              context: context,
              builder: (context) {
                return AlertDialog(
                  title: Text("Confirmation"),
                  content: SingleChildScrollView(
                    child: ListBody(
                      children: <Widget>[
                        Text("The phone number entered is: $phoneNo"),
                      ],
                    ),
                  ),
                  actions: <Widget>[
                    FlatButton(
                      child: Text("Cancel"),
                      onPressed: () {
                        Navigator.pop(context);
                      },
                    ),
                    FlatButton(
                      child: Text("Continue"),
                      onPressed: () {
                        prefShare.spNumberSetter(phoneNo);
                        Navigator.push(
                            context,
                            MaterialPageRoute(
                                builder: (context) => ButtonScreen()));
                      },
                    ),
                  ],
                );
              },
            );
          },
        ),
        body: Padding(
          padding: EdgeInsets.all(8.0),
          child: Column(
            children: [
              SizedBox(
                height: 150.0,
              ),
              TextField(
                  style: TextStyle(color:  Colors.black,),
                  decoration: InputDecoration(

                    hintText: "Your emergency contact number",
                    hintStyle: TextStyle(color:  Colors.black,),
                    enabledBorder: UnderlineInputBorder(
                      borderSide: BorderSide(color:  Colors.black,),
                    ),
                    focusedBorder: UnderlineInputBorder(
                      borderSide: BorderSide(color:  Colors.black,),
                    ),
                  ),
                  keyboardType: TextInputType.phone,
                  autofocus: true,
                  cursorColor: Colors.black,
                  onChanged: (text) {
                    phoneNo = text;
                  },
                ),

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

【问题讨论】:

    标签: android flutter dart


    【解决方案1】:

    我认为你的问题来自这里:prefShare.spAppGetter();

    由于 Future&lt;bool&gt; spAppGetter() async {...} 是一个返回未来的异步函数,您应该使用await 调用它,这样您就可以确保在调用bool status = prefShare.isNew ?? false; 之前spAppGetter 已经完成 为此,您也应该将主要功能标记为异步。你的主要功能看起来像这样:

    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await prefShare.spAppGetter();
      bool status = prefShare.isNew ?? false;
      prefShare.spAppNew();
      runApp(
        MaterialApp(home: status == true ? ButtonScreen() : Home()),
      );
    }
    

    【讨论】:

    • 谢谢。你能指出我可以了解更多关于 Futures、async 和 await 的来源吗?只有在可能的情况下。谢谢。
    • 很高兴能帮上忙,我认为在 dart docs 中有很好的解释。async/await
    【解决方案2】:

    你没有使用getBool("isLoggedIn")的状态

    void main() {
      WidgetsFlutterBinding.ensureInitialized();
      prefShare.spAppGetter();// it will actually tell you that you if user logged in or not
      bool status = prefShare.isNew ?? false;
      prefShare.spAppNew(); // this should be where the user once logged in.
      runApp(
        MaterialApp(home: status == true ? ButtonScreen() : Home()),
      );
    }
    

    应该是这样的

    void main() async{
      WidgetsFlutterBinding.ensureInitialized();
        bool status =await SharedPreferences.getInstance().getBool('isLoggedIn');
        runApp(
        MaterialApp(home: status == true ? ButtonScreen() : Home()),
      );
    }
    

    并在此处保存登录状态

      FlatButton(
                              child: Text("Continue"),
                              onPressed: () {
                                prefShare.spNumberSetter(phoneNo);
    SharedPreferences.getInstance().setBool('isLoggedIn');
                                Navigator.push(
                                    context,
                                    MaterialPageRoute(
                                        builder: (context) => ButtonScreen()));
                              },
                            ),
    

    你还应该研究命名约定

    【讨论】:

    • 你的建议帮助我解决了这个问题。谢谢。你知道我在哪里可以了解异步和等待吗?我是初学者。
    • 如果我的答案是正确的,那么请接受答案,以便其他人知道。在此处了解 async 和 await dart.dev/codelabs/async-await
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-26
    • 1970-01-01
    • 2019-07-21
    • 2021-10-25
    • 1970-01-01
    • 2020-12-14
    相关资源
    最近更新 更多