【问题标题】:Why is the redirect wrong page from main page?为什么从主页重定向错误的页面?
【发布时间】:2022-01-20 00:21:53
【问题描述】:

这是我的 main.dart 页面代码

import 'package:cwc/OnBoard/landing_page.dart';
import 'package:cwc/OnBoard/login_page.dart';
import 'package:cwc/ui/Home/home_page.dart';
import 'package:cwc/ui/signup/signup_page.dart';
import 'package:firebase_core/firebase_core.dart';

// import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';

import 'ApiManager/api_magager.dart';
import 'ApiManager/preference.dart';

// import 'Delete/delete.dart';
import 'Delete/delete.dart';
import 'OnBoard/enter_details.dart';
import 'OnBoard/sign_up_page.dart';
import 'OnBoard/varify_email_page.dart';
import 'constants/constants.dart';

Widget initialRoute = HomePage();

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  var token = await Preferences.loadData("token");
  var name = await Preferences.loadData("name");
  var zipCode = await Preferences.loadData("zipCode");
  var city = await Preferences.loadData("city");
  var state = await Preferences.loadData("state");
  var country = await Preferences.loadData("country");
  var status = await Preferences.loadData("status");
  var image = await Preferences.loadData("image");

// Preferences.clearData('token');
  print("name -- $name");
  print("zipCode -- $zipCode");
  print("city -- $city");
  print("state -- $state");
  print("country -- $country");
  print("token -- $token");
  print("status -- $status");
  print("image -- $image");

  if (token == null || token == "null") {                 // my first condition 
    initialRoute = LoginPage();
  } else if (token != null || token != "null") {          // my second condition
    if ((name == null || name == "null") ||
        (zipCode == null || zipCode == "null") ||
        (city == null || city == "null") ||
        (state == null || state == "null") ||
        (country == null || country == "null")) {
      initialRoute = EnterDetails();
    }
  } else if ((token != null || token != "null") &&        // my third condition
      (name != null || name != "null")) {
    print("status1 -- $status");
    if ((status == null || status == "null" || status == "inactive")) {
      initialRoute = LandingPage();
    }
  }
  await Firebase.initializeApp();

  runApp(const MyApp());
}

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  ApiManager apiManager = ApiManager();
 
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: initialRoute,
      // home:SignInScreen(),
    );
  }
}

这是我提到的三个条件的代码。我的第一个和第二个条件工作正常,但第三个条件不能正常工作我不知道为什么。 我第三个条件是我的令牌和名称不为空,然后再次检查我的状态是否等于“非活动”,那么它应该在我的登录页面上重定向。但是这些东西不起作用。重新加载项目时,这应该可以工作。

在此屏幕截图中,姓名、州、城市、国家不为空,并且我的状态为非活动状态。现在当我重新加载颤振项目时,它应该被重定向到 LandingPage 但它正在主页上

【问题讨论】:

    标签: flutter dart async-await flutter-test dart-null-safety


    【解决方案1】:

    在您使用过或无处不在的第二个条件中,如果名称、邮政编码、城市或国家/地区中的任何一个为空,它将认为条件 2 为真,并且永远不会检查条件 3。

    (name == null || name == "null") || 
    

    我建议您将|| 替换为&amp;&amp;。示例:

    name == null || name == "null") && (zipCode == null || zipCode == "null") &&(city == null || city == "null") &&
    

    【讨论】:

    • 不,它不工作我上传截图时我的名字,邮政编码,州,城市,国家不为空,也许它没有检查你可以在截图中看到的第三个代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 2023-03-10
    • 2017-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多