Home > Enterprise >  Unhandled Exception: PlatformException(null-error, Host platform returned null value for non-null re
Unhandled Exception: PlatformException(null-error, Host platform returned null value for non-null re

Time:01-04

hello I hope you are well, I have the following problem, when I run my app in flutter I get the following ERROR: E/flutter ( 6922): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException( null-error, Host platform returned null value for non-null return value., null, null), as I am working with firebase, but I have searched a lot and the truth is that the solutions that come up have not worked for me or I do not know if it is me who I implemented it incorrectly, soon I will show you the code, thank you very much for stopping to read this publication. I also forgot to mention I'm working on visual studio code

MAIN CODE(),


import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'src/Base/Views/BaseView.dart';
import 'src/Feactures/Presentation/Shared/StateProviders/LoadingStateProvider.dart';
import 'src/Feactures/Presentation/Shared/StateProviders/UserStateProvider.dart';
import 'src/Colors/colors.dart';
import 'src/Routes/routes.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:firebase_core/firebase_core.dart';

void main() => runApp(AppState());

class AppState extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => LoadingStateProvider()),
        ChangeNotifierProvider(create: (_) => UserStateProvider())
      ],
      child: MyAppUserState(),
    );
  }
}

class MyAppUserState extends StatelessWidget with BaseView {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
        future: coordinator.start(),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.hasData) {
            return MyApp(initialRoute: snapshot.data);
          } else {
            return CircularProgressIndicator();
          }
        });
  }
}

class MyApp extends StatelessWidget {
  final String _initialRoute;

  MyApp({required String initialRoute}) : _initialRoute = initialRoute;

  @override
  Widget build(BuildContext context) {
    Firebase.initializeApp();

    return MaterialApp(
      debugShowCheckedModeBanner: false,
      routes: routes,
      initialRoute: _initialRoute,
      theme: ThemeData(
          scaffoldBackgroundColor: Colors.white,
          primaryColor: orange,
          appBarTheme:
              const AppBarTheme(iconTheme: IconThemeData(color: Colors.black))),
      localizationsDelegates: const [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: const [
        Locale('en', ''), // English, no country code
        Locale('es', ''), // Spanish, no country code
      ],
    );
  }
}

I really expected the app to run fine but it sends that error

CodePudding user response:

You probably don't want to initialize Firebase in the build method. Try doing this instead (which is in the FlutterFire documentation):

void main() async {

  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();

  runApp();
}

CodePudding user response:

1. Add to pubspec.yaml: firebase_core :recent version

2. add to main.dart, If you already have a main method, you can replace it with the following code and it might work for you

import 'package:firebase_core/firebase_core.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(AppState());
}
  • Related