Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· OpenAIAi2 WildChat-4.8M (AI2)

its showimg grey screen when i open app after flutter build apk but when i run it on…

  1. User

    its showimg grey screen when i open app after flutter build apk but when i run it on emulator it doesnt show the grey screen. it only shows when i build apk and after login when im supposed to enter home page it shows grey screen // ignore_for_file: override_on_non_overriding_member, annotate_overrides, prefer_const_constructors

    import 'package:flutter/material.dart'; import 'package:healthchats/body.dart'; import 'package:healthchats/constants.dart';

    import 'package:healthchats/navigationbar.dart'; import 'package:healthchats/service/auth-service.dart'; import 'helper/helperFunction.dart';

    import 'backgroundgradient.dart';

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

    State createState() => _HomeState(); }

    class _HomeState extends State { String userName = ""; String email = ""; AuthService authService = AuthService(); @override void initState() { super.initState(); gettingUserData(); }

    gettingUserData() async { await helperFunctions.getUserEmailFromSF().then((value) { setState(() { email = value!; }); }); await helperFunctions.getUserNameFromSF().then((val) { setState(() { userName = val!; }); }); }

    @override static final scaffoldKey = GlobalKey();

    Widget build(BuildContext context) { return Scaffold( key: scaffoldKey, drawer: navbar(context, userName, email), // bottomNavigationBar: bottomnav(),

        // appBar: BuildAppBar(),
        body: Container(
          alignment: Alignment.center,
          decoration: BoxDecoration(
              gradient: LinearGradient(
            colors: const [
              kbackgroundColor2,
              Color.fromARGB(255, 238, 236, 232)
            ],
          )),
          child: Body(
            scaffoldKey: scaffoldKey,
          ),
        ),
        backgroundColor:
            Colors.transparent, // set background color to transparent
        resizeToAvoidBottomInset: false);
    

    } }

  2. Assistant

    The issue might be related to the initialization of SharedPreferences in helper functions when running the app on a real device. But without the helperFunctions code, it is impossible to provide an exact solution for that.

    However, here's a suggestion: try to handle the initialization and async actions surrounding SharedPreferences properly.

    1. Make sure you have added the following dependencies in your pubspec.yaml file:
    dependencies:
      shared_preferences: ^2.0.8
    
    1. Modify your helper functions as follows:
    import 'package:shared_preferences/shared_preferences.dart';
    
    class HelperFunctions {
      static String? userEmailKey = "USEREMAIL";
      static String? userNameKey = "USERNAME";
    
      static Future<bool> saveUserEmail(String userEmail) async {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        return await prefs.setString(userEmailKey, userEmail);
      }
    
      static Future<bool> saveUserName(String userName) async {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        return await prefs.setString(userNameKey, userName);
      }
    
      static Future<String?> getUserEmailFromSF() async {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        return prefs.getString(userEmailKey);
      }
    
      static Future<String?> getUserNameFromSF() async {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        return prefs.getString(userNameKey);
      }
    }
    
    1. In your _initState() function of the _HomeState, use the modified helper functions like this:
      @override
      void initState() {
        super.initState();
        gettingUserData();
      }
    
      gettingUserData() async {
        String? userEmail = await HelperFunctions.getUserEmailFromSF();
        String? userName = await HelperFunctions.getUserNameFromSF();
    
        if (userEmail != null && userName != null) {
          setState(() {
            email = userEmail;
            userName = userName;
          });
        }
      }
    
    1. Build the APK and test it on a real device. If it still doesn't work, try debugging the code with print statements to check what's happening during runtime. You can use print() to log the values of the variables and the flow of the code.
  3. User

    its showimg grey screen when i open app after flutter build apk but when i run it on emulator it doesnt show the grey screen. it only shows when i build apk and after login when im supposed to enter home page it shows grey screen // ignore_for_file: override_on_non_overriding_member, annotate_overrides, prefer_const_constructors

    import ‘package:flutter/material.dart’; import ‘package:healthchats/body.dart’; import ‘package:healthchats/constants.dart’;

    import ‘package:healthchats/navigationbar.dart’; import ‘package:healthchats/service/auth-service.dart’; import ‘helper/helperFunction.dart’;

    import ‘backgroundgradient.dart’;

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

    State createState() => _HomeState(); }

    class _HomeState extends State { String userName = “”; String email = “”; AuthService authService = AuthService(); @override void initState() { super.initState(); gettingUserData(); }

    gettingUserData() async { await helperFunctions.getUserEmailFromSF().then((value) { setState(() { email = value!; }); }); await helperFunctions.getUserNameFromSF().then((val) { setState(() { userName = val!; }); }); }

    @override static final scaffoldKey = GlobalKey();

    Widget build(BuildContext context) { return Scaffold( key: scaffoldKey, drawer: navbar(context, userName, email), // bottomNavigationBar: bottomnav(),

    // appBar: BuildAppBar(), body: Container( alignment: Alignment.center, decoration: BoxDecoration( gradient: LinearGradient( colors: const [ kbackgroundColor2, Color.fromARGB(255, 238, 236, 232) ], )), child: Body( scaffoldKey: scaffoldKey, ), ), backgroundColor: Colors.transparent, // set background color to transparent resizeToAvoidBottomInset: false); } } this is my helper function code // ignore_for_file: file_names, camel_case_types

    import 'package:shared_preferences/shared_preferences.dart';

    class helperFunctions { //keys

    static String userLoggedInkey = "LOGGEDINKEY"; static String userNameKey = "USERNAMEKEY"; static String userEmailKey = "USEREMAILKEY";

    // saving the data with the shared preferences

    static Future saveUserLoggedInStatus(bool isUserLoggedIn) async { SharedPreferences sf = await SharedPreferences.getInstance(); return await sf.setBool(userLoggedInkey, isUserLoggedIn); }

    static Future saveUsernameSF(String userName) async { SharedPreferences sf = await SharedPreferences.getInstance(); return await sf.setString(userNameKey, userName); }

    static Future saveUserEmailSF(String userEmail) async { SharedPreferences sf = await SharedPreferences.getInstance(); return await sf.setString(userEmailKey, userEmail); }

    //getting the data from the shared preferences

    static Future<bool?> getUserLoggedInStatus() async { SharedPreferences sf = await SharedPreferences.getInstance(); return sf.getBool(userLoggedInkey); } static Future<String?> getUserEmailFromSF() async { SharedPreferences sf = await SharedPreferences.getInstance(); return sf.getString(userEmailKey); } static Future<String?> getUserNameFromSF() async { SharedPreferences sf = await SharedPreferences.getInstance(); return sf.getString(userNameKey); } }

  4. Assistant

    Thanks for sharing your helper function code. Please have the function names in your helper class follow the camelCase naming convention, i.e., start with a lowercase letter instead of an uppercase letter.

    Then, try to update your _wrapperState as follows:

    import 'helper/helperFunction.dart' as helperFunctions; // Add an alias for the helper functions
    
    class _HomeState extends State<Home> {
      String userName = "";
      String email = "";
      AuthService authService = AuthService();
    
      @override
      void initState() {
        super.initState();
        gettingUserData();
      }
    
      gettingUserData() async {
        await helperFunctions.HelperFunctions.getUserEmailFromSF().then((value) {
          if (value != null) {
            setState(() {
              email = value;
            });
          }
        });
        await helperFunctions.HelperFunctions.getUserNameFromSF().then((val) {
          if (val != null) {
            setState(() {
              userName = val;
            });
          }
        });
      }
    
      @override
      static final scaffoldKey = GlobalKey<ScaffoldState>();
    
      Widget build(BuildContext context) {
        ...
    

    Once you've updated your code, try building the APK and test it on a real device. If it still doesn't work, debug the code with print statements to see what's going on during runtime. For example, add print statements in your gettingUserData method:

    gettingUserData() async {
      await helperFunctions.HelperFunctions.getUserEmailFromSF().then((value) {
        if (value != null) {
          print("Email: $value");
          setState(() {
            email = value;
          });
        }
      });
      await helperFunctions.HelperFunctions.getUserNameFromSF().then((val) {
        if (val != null) {
          print("Username: $val");
          setState(() {
            userName = val;
          });
        }
      });
    }
    

    Check the logs to see if the email and username are being fetched correctly. This should help you identify the issue causing the grey screen on your device.

  5. User

    how can i check logs

  6. Assistant

    To check the logs (print statements) in a Flutter app running on a physical device, follow these steps:

    1. Make sure your device is connected to your computer and it appears in the list when you run flutter devices.

    2. Open your terminal/command prompt window, navigate to your Flutter project directory, and execute the following command:

    flutter logs
    

    This command will start listening to logs from the connected Flutter devices. Any console logs or print statements generated on the device during the app's runtime will be displayed in the terminal/command prompt window.

    1. Run your app on the device either by building the APK and running the APK, or running the following command in a separate terminal/command prompt window to run the app in debug mode:
    flutter run
    
    1. As you interact with your app, logs will be displayed in the terminal/command prompt window where the flutter logs command was executed. Look for the print statements you added like "Email: example@example.com" and "Username: username" to understand the flow of your code and any potential issues.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.