fireship-io / fireship.io

Build and ship your app faster https://fireship.io
3.57k stars 1.31k forks source link

courses/flutter-firebase/auth-stream/ #829

Open utterances-bot opened 2 years ago

utterances-bot commented 2 years ago

Auth Stream

Listen to the current Firebase user

https://fireship.io/courses/firestore-data-modeling/basics-read-documents/

eikowagenknecht commented 2 years ago

Snippet code does not match what is shown in the video.

import 'package:quizapp/shared/shared.dart'; < Not found LoadingScreen() < Not found ErrorMessage < Not found

Am I missing something here?

tappella commented 2 years ago

Hi @eikowagenknecht, you are right. The code snippet considers the LoadingScreen and the ErrorMessage being both Custom Widgets. For an actual app, that's what you will probably do it, but in the video lesson the instructor is just using Text Widgets in place of those.

Try creating 2 Custom Stateless Widgets, name them LoadingScreen and ErrorMessage and make them both return a simple Text Widget with according text. Then your Code Editor will probably suggest to import the respective files you created the Custom Widgets.

jaedag commented 2 years ago

If you are running into the error that says Null check operator used on a null value then copy this code for your main.dart file. Took me while to find this answer

import 'package:fireship_quizapp/home/home.dart';
import 'package:fireship_quizapp/routes.dart';
import 'package:fireship_quizapp/theme.dart';
import 'package:flutter/material.dart';
// Import the firebase_core plugin
import 'package:firebase_core/firebase_core.dart';
import 'services/firebase_options.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  runApp(const App());
}

/// We are using a StatefulWidget such that we only create the [\Future] once,
/// no matter how many times our widget rebuild.
/// If we used a [\StatelessWidget], in the event where [\App] is rebuilt, that
/// would re-initialize FlutterFire and make our application re-enter loading state,
/// which is undesired.
///
class App extends StatefulWidget {
  const App({Key? key}) : super(key: key);

  @override
  State<App> createState() => _AppState();
}

class _AppState extends State<App> {
  /// The future is part of the state of our widget. We should not call `initializeApp`
  /// directly inside [build].
  final Future<FirebaseApp> _initialization = Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      routes: appRoutes,
      theme: appTheme,
      home: FutureBuilder(
        // Initialize FlutterFire:
        future: _initialization,
        builder: (context, snapshot) {
          // Check for errors
          if (snapshot.hasError) {
            return const Text('error');
          }

          // Once complete, show your application
          if (snapshot.connectionState == ConnectionState.done) {
            return const HomeScreen();
          }

          // Otherwise, show something whilst waiting for initialization to complete
          return const Text('loading');
        },
      ),
    );
  }
}
jaedag commented 2 years ago

And also delete the '/' route from the appRoutes object

shubhjagani commented 2 years ago

When trying to flutter run i get this issue

Error: Required named parameter 'auth' must be provided. _factory.delegateFor( ^

FAILURE: Build failed with an exception.

SinaParvaresh commented 2 years ago

Does Anybody get this error?

The following FirebaseException was thrown building HomeScreen(dirty): [core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()