0

This is my main.dart page code

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(),
    );
  }
}

This is Code I mentioned three condition. my first and second condition working fine but third condition not working fine I dont know why. I third condtion my token and name is not null then its check again if my status is equal to "inactive" then it should be redirect on my landing page. But These things not working. this things should be work when reload the project.

enter image description here

In this Screenshot token,name,state,city,country not null and my status is inactive. now when I reload the flutter project then it should be redirect on LandingPage But it is going on homepage

isherwood
  • 58,414
  • 16
  • 114
  • 157
Deepak
  • 1,664
  • 3
  • 19
  • 52

1 Answers1

0

In your second condition you have used or everywhere so if any of name, zip code, city or country are null, it will consider condition 2 to be true and will never check condition three.

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

I would suggest you to replace || with &&. Example:

name == null || name == "null") && (zipCode == null || zipCode == "null") &&(city == null || city == "null") &&
isherwood
  • 58,414
  • 16
  • 114
  • 157
Yash Bhansali
  • 420
  • 2
  • 6
  • No its not working I Upload screenshot when my name, zipcode,state,city,country not null and maybe it is not checking third codition U can see in screenshot – Deepak Jan 19 '22 at 16:13