【问题标题】:How to go from one page to another depending on the data they receive when opening the app如何根据打开应用程序时收到的数据从一个页面转到另一个页面
【发布时间】:2022-01-09 05:55:02
【问题描述】:

我正在尝试制作一个具有三种类型用户的应用程序:学生、教师和家长,我所做的是在登录时使用 shared_preferences 保存用户令牌的 ID,我希望它在输入时搜索数据库应用程序,根据用户类型,转到相应页面

目前我正在尝试这个

import 'package:app_plantel/padres_page.dart';
import 'package:app_plantel/profesores_page.dart';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {

  Widget build(BuildContext context) {
    isLogged(context);
    return new Scaffold(
      body: Center(
        child: Icon(
          Icons.beach_access,
        ),
      ),
    );
  }
}

Future<String> _returnValue() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  final token = await prefs.getString("token");
  return token;
}

Future isLogged(context) async {

  await FirebaseFirestore.instance
    .collection('Padres')
    .doc(_returnValue().toString())
    .get()
    .then((DocumentSnapshot documentSnapshot) async {
      if (documentSnapshot.exists) {
        Navigator.of(context).push(MaterialPageRoute(builder: (context)=> PadresPage()));
      } else {
        await FirebaseFirestore.instance
        .collection('Alumnos')
        .doc(_returnValue().toString())
        .get()
        .then((DocumentSnapshot documentSnapshot) async {
          if (documentSnapshot.exists) {
            Navigator.of(context).push(MaterialPageRoute(builder: (context)=>  AlumnosPage()));
          } else {
            await FirebaseFirestore.instance
            .collection('Profesores')
            .doc(_returnValue().toString())
            .get()
            .then((DocumentSnapshot documentSnapshot) async {
              if (documentSnapshot.exists) {
                Navigator.of(context).push(MaterialPageRoute(builder: (context)=> ProfesoresPage()));
              } else {
                Navigator.of(context).push(MaterialPageRoute(builder: (context)=> LoginPage()));
              }
            });
          }
        });
      }
    });
}

但它给我带来了很多错误:

E/flutter (13461): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Navigator operation requested with a context that does not include a Navigator.
E/flutter (13461): The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.
E/flutter (13461): #0      Navigator.of.<anonymous closure> (package:flutter/src/widgets/navigator.dart:2553:9)
E/flutter (13461): #1      Navigator.of (package:flutter/src/widgets/navigator.dart:2560:6)
E/flutter (13461): #2      isLogged.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:app_plantel/main.dart:53:27)
E/flutter (13461): #3      isLogged.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:app_plantel/main.dart:49:19)
E/flutter (13461): #4      _rootRunUnary (dart:async/zone.dart:1436:47)
E/flutter (13461): #5      _CustomZone.runUnary (dart:async/zone.dart:1335:19)
E/flutter (13461): <asynchronous suspension>
E/flutter (13461): #6      isLogged.<anonymous closure>.<anonymous closure> (package:app_plantel/main.dart:45:13)
E/flutter (13461): <asynchronous suspension>
E/flutter (13461): #7      isLogged.<anonymous closure> (package:app_plantel/main.dart:37:9)
E/flutter (13461): <asynchronous suspension>
E/flutter (13461): #8      isLogged (package:app_plantel/main.dart:29:3)
E/flutter (13461): <asynchronous suspension>
E/flutter (13461):

我想知道如何解决它或他们建议能够做到的其他方法

【问题讨论】:

    标签: firebase flutter


    【解决方案1】:

    抛出异常是因为您的Scaffold 上方没有MaterialAppCupertinoApp 小部件,并且导航需要它。像这样的:

    return const MaterialApp(
      home: Scaffold(
        body: Center(
          child: Icon(
            Icons.beach_access,
          ),
        ),
      )
    );
    

    此外,您的isLogged 函数是async,但您不能在build 方法中等待它。考虑使用FutureBuilder 等待数据库响应,然后再开始构建主小部件。

    【讨论】:

      【解决方案2】:

      你传入 isLogged 方法的上下文没有被导航器包裹。 可以设置一个全局变量final _navKey = GlobalKey&lt;NavigatorState&gt;(); 然后用 Material 应用包裹你的 UI 并设置 Material 应用导航键

      const MaterialApp(
        home: Scaffold(
          body: Center(
            child: Icon(
              Icons.beach_access,
            ),
          ),
        ),
      navigatorKey: _navKey
      );
      

      最后使用key检索导航器上下文并在调用导航器时使用_navKey.currentState?.context

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多