Flutter - Trying to build a future context to check if user is logged in, but my build method has an error

Viewed 4631

Fairly newish to flutter, and I have an app that can succesfully log into Firebase. However now I'm trying to make it so the app skips the registration page if the user is already registered. I started a new template for this post.

I used this post: How to correctly check if a user is logged in for the inspiration, but mine seems to throw an error. I'm using VSCODE if that makes a difference:

import 'package:buildfuture/FirstPage.dart';
import 'package:buildfuture/SecondPage.dart';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';

void main() {
  runApp(MyApp());
}

class MyApp implements StatelessWidget {

  @override 
    Widget build() {
      return FutureBuilder<FirebaseUser>( 
        future: FirebaseAuth.instance.currentUser(),
        builder: (BuildContext context, AsyncSnapshot<FirebaseUser> snapshot) {

          if (snapshot.hasData) {
            FirebaseUser user = snapshot.data;
            return First();
          }
           return Second();
          }
      );
    }
}

I don't understand the error:

'Object.toString' ('String Function()') isn't a valid override of 'Diagnosticable.toString' ('String Function({DiagnosticLevel minLevel})').

and

'MyApp.build' ('Widget Function()') isn't a valid override of 'StatelessWidget.build' ('Widget Function(BuildContext)').

Just in case I've also included FirstPage.dart. SecondPage.dart is exactly the same except the class is named Second

import 'package:flutter/material.dart';

class First extends StatelessWidget {

  @override
  Widget build(BuildContext context){
    return new Container(
      child: new Center(
        child: new Icon(Icons.accessibility_new, size: 150.0, color: Colors.brown)
      )
    );
  }
}
3 Answers

The mistake is the line class MyApp implements StatelessWidget

You should use extends keyword instead implements.

Try:

class MyApp extends StatelessWidget{ // your class body ... }

and also build method signature must include a BuildContext parameter.

@override
Widget build(BuildContext context){ return ... }

'MyApp.build' ('Widget Function()') isn't a valid override of 'StatelessWidget.build' ('Widget Function(BuildContext)').

Bad override signature.

Your build() method into MyApp class must to be build(BuildContext context):

@override 
Widget build(BuildContext context) {
  return FutureBuilder<FirebaseUser>( 
    future: FirebaseAuth.instance.currentUser(),
    builder: (BuildContext context, AsyncSnapshot<FirebaseUser> snapshot) {

      if (snapshot.hasData) {
        FirebaseUser user = snapshot.data;
        return First();
      }
       return Second();
      }
  );
}

If you place a breakpoint at line FirebaseUser user = snapshot.data; what is the value of snapshot.data ?

sigh.

I didn't see... in my original code I "implement" rather than "extend". It's been killing me for 2 days.

Related