Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Executing Firebase app 3 times results in crash #1912

Closed
srawlins opened this issue Jun 2, 2021 · 6 comments
Closed

Executing Firebase app 3 times results in crash #1912

srawlins opened this issue Jun 2, 2021 · 6 comments
Assignees
Labels
area-packages P1 A high priority bug; for example, a single project is unusable or has many test failures type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Milestone

Comments

@srawlins
Copy link
Member

srawlins commented Jun 2, 2021

Take this app:

import 'package:flutter/material.dart';
import 'package:firebase/firebase.dart';
import 'package:firebase/firestore.dart' as fs;

void main() async {
  try {
    initializeApp(
      apiKey: ...,
      authDomain: ...,
      projectId: ...,
      storageBucket: ...,
      messagingSenderId: ...,
      appId: ...,
      measurementId: ...
    );
  } catch (ex) {
    print('App was already initialized...');
  }
    
  fs.Firestore store = firestore();
  fs.CollectionReference ref = store.collection('bandnames');

  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          title: Text('Potential names for my band'),
        ),
        body: MyWidget(ref.onSnapshot),
        floatingActionButton: AuthFab(),
      ),
    ),
  );
}

class MyWidget extends StatelessWidget {
  final Stream<fs.QuerySnapshot> stream;

  const MyWidget(this.stream);

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<fs.QuerySnapshot>(
        stream: stream,
        builder: (context, snapshot) {
          if (snapshot.hasError) {
            return Center(
              child: Text('$snapshot.error'),
            );
          } else if (!snapshot.hasData) {
            return Center(
              child: SizedBox(
                width: 50,
                height: 50,
                child: CircularProgressIndicator(),
              ),
            );
          }
          return ListView.builder(
              itemCount: snapshot.data!.docs.length,
              itemBuilder: (context, i) {
                return ListTile(
                  leading: Text('${snapshot.data!.docs[i].data()['votes']}'),
                  title: Text('${snapshot.data!.docs[i].data()['name']}'),
                );
              });
        });
  }
}

class AuthFab extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => AuthFabState();
}

class AuthFabState extends State<AuthFab> {
  Future<UserCredential>? authFuture;

  @override
  Widget build(BuildContext build) {
    if (authFuture == null) {
      return FloatingActionButton(
        onPressed: () {
          setState(() {
            authFuture = auth().signInWithEmailAndPassword(
              'blah@blah.com',
              'password',
            );
          });
        },
        child: Icon(Icons.person_outline),
      );
    }

    return FloatingActionButton(
      onPressed: () {},
      child: FutureBuilder<UserCredential>(
        future: authFuture,
        builder: (context, snapshot) {
          if (snapshot.hasError) {
            return Icon(Icons.error);
          } else if (!snapshot.hasData) {
            return Icon(Icons.refresh);
          }

          return Icon(Icons.person);
        },
      ),
    );
  }
}

running the app 3 times, without changing the code, results in this error:

Uncaught (in promise) Error: Expected a value of type 'Firestore?', but got one of type 'Firestore'
    at Object.throw_ [as throw] (dart_sdk.js:5041)
    at Object.castError (dart_sdk.js:5014)
    at Object.cast [as as] (dart_sdk.js:5323)
    at dart.NullableType.new.as (dart_sdk.js:6830)
    at Expando.new._get (dart_sdk.js:118824)
    at Function.getInstance (<anonymous>:3609:17)
    at Object.firestore$0 [as firestore] (<anonymous>:3559:33)
    at main$ (<anonymous>:692:29)
    at main$.next (<anonymous>)
    at runBody (dart_sdk.js:37422)
    at Object._async [as async] (dart_sdk.js:37453)
    at Object.main$0 [as main] (<anonymous>:682:18)
    at main$ (<anonymous>:584:12)
    at main$.next (<anonymous>)
    at dart_sdk.js:37403
    at _RootZone.runUnary (dart_sdk.js:37274)
    at _FutureListener.thenAwait.handleValue (dart_sdk.js:32530)
    at handleValueCallback (dart_sdk.js:33057)
    at Function._propagateToListeners (dart_sdk.js:33095)
    at _Future.new.[_completeWithValue] (dart_sdk.js:32943)
    at dart_sdk.js:32194
    at _RootZone.runUnary (dart_sdk.js:37274)
    at _FutureListener.then.handleValue (dart_sdk.js:32530)
    at handleValueCallback (dart_sdk.js:33057)
    at Function._propagateToListeners (dart_sdk.js:33095)
    at _Future.new.[_completeWithValue] (dart_sdk.js:32943)
    at async._AsyncCallbackEntry.new.callback (dart_sdk.js:32964)
    at Object._microtaskLoop (dart_sdk.js:37526)
    at _startMicrotaskLoop (dart_sdk.js:37532)
    at dart_sdk.js:33303
@srawlins
Copy link
Member Author

srawlins commented Jun 2, 2021

CC @RedBrogdon This is the app you shared.

I cannot make heads or tails of that error. Is there a way to de-obfuscate it? One possibility is that there are "two types" called "Firestore". And "two types" may mean two actually different types declared in two different places, or a single type which is imported in two different ways.

@RedBrogdon RedBrogdon added P1 A high priority bug; for example, a single project is unusable or has many test failures area-packages type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Jun 17, 2021
@RedBrogdon RedBrogdon added this to the On Deck milestone Jun 17, 2021
@srawlins
Copy link
Member Author

Potential related issue: dart-lang/sdk#37259, hard to say though...

@srawlins
Copy link
Member Author

@nshahan deduced that this may be related to an Expando + Hot Restart bug which should be fixed in recent Flutter beta. Bug does indeed seem fixed with 2.14.0-301.2.beta.

I'll leave this open though until it is fixed in stable. :/

@RedBrogdon
Copy link
Contributor

@srawlins, has this one been resolved with the latest SDK release?

@srawlins
Copy link
Member Author

srawlins commented Oct 8, 2021

Not sure. I want to fix #2033 and then circle back here.

@srawlins
Copy link
Member Author

OK this looks fixed. I can run the bandnames app over and over and no crash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-packages P1 A high priority bug; for example, a single project is unusable or has many test failures type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

2 participants