简体   繁体   English

无法使用 Flutter 应用程序访问 Firebase 后端

[英]unable to reach firebase backend with flutter app

I'm starting out with firebase and flutter.我从 firebase 和 flutter 开始。 I want to start from working code, so I've implemented the baby names codelab.我想从工作代码开始,所以我实现了婴儿名字代码实验室。 It worked.有效。 And then stopped working.然后停止工作。 I tried to update the codelab to use the latest versions of firestore.我尝试更新 codelab 以使用最新版本的 firestore。 Still no longer working.仍然不再工作。 This has happened to me with a different app.这发生在我身上,有一个不同的应用程序。 Even though I live in Thailand, my internet connection is pretty good.即使我住在泰国,我的互联网连接也很好。

this is the updated baby names codelab这是更新的婴儿名字代码实验室

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';


void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

final dummySnapshot = [
  {"name": "Filip", "votes": 15},
  {"name": "Abraham", "votes": 14},
  {"name": "Richard", "votes": 11},
  {"name": "Ike", "votes": 10},
  {"name": "Justin", "votes": 1},
];

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Baby Names',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() {
    return _MyHomePageState();
  }
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Baby Name Votes')),
      body: _buildBody(context),
    );
  }

  Widget _buildBody(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
      stream: FirebaseFirestore.instance.collection('baby').snapshots(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) return LinearProgressIndicator();

        return _buildList(context, snapshot.data.docs);
      },
    );
  }
  Widget _buildList(BuildContext context,  List<DocumentSnapshot> snapshot) {
    return ListView(
      padding: const EdgeInsets.only(top: 20.0),
      children: snapshot.map((data) => _buildListItem(context, data)).toList(),
    );
  }

  Widget _buildListItem(BuildContext context, DocumentSnapshot data) {
    final record = Record.fromSnapshot(data);

    return Padding(
      key: ValueKey(record.name),
      padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
      child: Container(
        decoration: BoxDecoration(
          border: Border.all(color: Colors.grey),
          borderRadius: BorderRadius.circular(5.0),
        ),
        child: ListTile(
          title: Text(record.name),
          trailing: Text(record.votes.toString()),
            onTap: () => record.reference.update({'votes': FieldValue.increment(1)})

        ),
      ),
    );
  }
}

class Record {
  final String name;
  final int votes;
  final DocumentReference reference;

  Record.fromMap(Map<String, dynamic> map, {this.reference})
      : assert(map['name'] != null),
        assert(map['votes'] != null),
        name = map['name'],
        votes = map['votes'];

  Record.fromSnapshot(DocumentSnapshot snapshot)
      : this.fromMap(snapshot.data(), reference: snapshot.reference);

  @override
  String toString() => "Record<$name:$votes>";
}

this is the relevant section of pubspec.yaml这是 pubspec.yaml 的相关部分

dependencies: flutter: sdk: flutter依赖项:颤振:sdk:颤振

cupertino_icons: ^1.0.0 cloud_firestore: ^0.14.3 firebase_core: ^0.5.2 cupertino_icons:^1.0.0 cloud_firestore:^0.14.3 firebase_core:^0.5.2

here is the relevant (I hope) section from flutter run --verbose这是 flutter run --verbose 的相关(我希望)部分

[        ] I/ProviderInstaller(30660): Installed default security provider GmsCore_OpenSSL
[  +91 ms] D/ConnectivityManager(30660): requestNetwork; CallingUid : 10182, CallingPid : 30660
[  +76 ms] W/ware.baby_name(30660): Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
[        ] W/ware.baby_name(30660): Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
[+5234 ms] D/ViewRootImpl@7513633[MainActivity](30660): ViewPostIme pointer 0
[  +73 ms] D/ViewRootImpl@7513633[MainActivity](30660): ViewPostIme pointer 1
[+4565 ms] W/Firestore(30660): (22.0.0) [OnlineStateTracker]: Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds
[        ] W/Firestore(30660): 
[        ] W/Firestore(30660): This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.
[+5138 ms] D/ConnectivityManager(30660): unregisterNetworkCallback; CallingUid : 10182, CallingPid : 30660
[  +28 ms] W/Firestore(30660): (22.0.0) [WriteStream]: (eed8101) Stream closed with status: Status{code=UNAVAILABLE, description=Channel shutdownNow invoked, cause=null}.
[  +12 ms] W/DynamiteModule(30660): Local module descriptor class for providerinstaller not found.
[        ] W/Firestore(30660): (22.0.0) [WatchStream]: (78299e7) Stream closed with status: Status{code=UNAVAILABLE, description=Channel shutdownNow invoked, cause=null}.
[  +11 ms] I/DynamiteModule(30660): Considering local module providerinstaller:0 and remote module providerinstaller:0
[        ] W/ProviderInstaller(30660): Failed to load providerinstaller module: No acceptable module found. Local version is 0 and remote version is 0.
[   +9 ms] D/ConnectivityManager(30660): requestNetwork; CallingUid : 10182, CallingPid : 30660
[+15025 ms] D/ConnectivityManager(30660): unregisterNetworkCallback; CallingUid : 10182, CallingPid : 30660
[  +10 ms] W/Firestore(30660): (22.0.0) [WriteStream]: (eed8101) Stream closed with status: Status{code=UNAVAILABLE, description=Channel shutdownNow invoked, cause=null}.
[   +4 ms] W/Firestore(30660): (22.0.0) [WatchStream]: (78299e7) Stream closed with status: Status{code=UNAVAILABLE, description=Channel shutdownNow invoked, cause=null}.

I see the section where it says the backend didn't respond in 30 seconds and could indicate a bad internet connection.我看到它说后端在 30 秒内没有响应并且可能表明互联网连接不良的部分。 but there is nothing wrong with my internet.但我的互联网没有任何问题。 it has been working fine.它一直工作正常。 To try and troubleshoot this, I originally set it up in southeast asia 2 because I'm in thailand.为了尝试解决此问题,我最初将其设置在东南亚 2 中,因为我在泰国。 When it stopped workint the next day.当它在第二天停止工作时。 i removed my project from firebase and rebuilt it in west coast usa.我从 firebase 中删除了我的项目,并在美国西海岸重建了它。 I also ran it with my vpn on in case that helped.我也用我的 vpn 运行它以防万一。 I then removed the project and put it back in southeast asia.然后我删除了该项目并将其放回东南亚。 none of this had any impact on this error.这些都没有对这个错误产生任何影响。

You might have just overlooked a step or messed something with the configuration at some point.您可能只是忽略了某个步骤或在某些时候弄乱了配置。 Try to start another project from scratch and see if problem persists.尝试从头开始另一个项目,看看问题是否仍然存在。

Problem solved.问题解决了。 I tried to use my phone as a hot spot to test if it was my network.我试着用我的手机作为热点来测试它是否是我的网络。 As soon as I did that, it started working.一旦我这样做了,它就开始工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM