【问题标题】:unable to reach firebase backend with flutter app无法使用颤振应用程序访问 Firebase 后端
【发布时间】:2020-11-16 12:18:16
【问题描述】:

我从 firebase 和 flutter 开始。我想从工作代码开始,所以我实现了婴儿名字 codelab。有效。然后停止工作。我尝试更新 codelab 以使用最新版本的 firestore。仍然不再工作。这在我身上发生了一个不同的应用程序。虽然我住在泰国,但我的互联网连接非常好。

这是更新后的婴儿名字代码实验室

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>";
}

这是 pubspec.yaml 的相关部分

依赖: 扑: sdk:颤动

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

这是来自 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}.

我看到它说后端在 30 秒内没有响应的部分,这可能表明互联网连接不良。但我的互联网没有任何问题。它一直运行良好。为了尝试解决此问题,我最初将其设置在东南亚 2 中,因为我在泰国。第二天停止工作时。我从 firebase 中删除了我的项目并在美国西海岸重建了它。如果有帮助,我还使用我的 vpn 运行它。然后我删除了该项目并将其放回东南亚。这些都不会对这个错误产生任何影响。

【问题讨论】:

    标签: firebase flutter google-cloud-firestore


    【解决方案1】:

    您可能只是忽略了一个步骤或在某些时候弄乱了配置。尝试从头开始另一个项目,看看问题是否仍然存在。

    【讨论】:

    • 好的。我删除了项目。创造了一个新的。从我的清单文件中获取路径名。确保 min sdk 为 21。禁用分析。从我的项目中删除了旧的 JSON 文件,并在下载后添加了新文件。确保 build.gradle 文件遵循了这些步骤。测试了我的网速。 350Mbps 下行,15Mbps 上行。 45 毫秒延迟(新加坡服务器)。在两种不同的平板电脑上尝试过。结果相同。我有一个 -verbose 运行,但它太大而无法发布。如何分享?
    • 一件事情发生了变化,当我第一次创建一个项目时,它提示我跟随直到完成,然后当我的应用程序第一次运行时它会连接到我的应用程序。这不再发生。我错过了连接到我的应用程序的步骤吗?你怎么能强制这种连接?
    • 我也遇到了同样的问题,但是如果我将手机连接改为使用 4g 而不是 wifi,它可以工作
    【解决方案2】:

    问题解决了。我试图用我的手机作为热点来测试它是否是我的网络。我一这样做,它就开始工作了。

    【讨论】:

      猜你喜欢
      • 2019-06-03
      • 1970-01-01
      • 2022-08-15
      • 2021-10-23
      • 2020-07-03
      • 2023-03-05
      • 1970-01-01
      • 2021-06-16
      • 1970-01-01
      相关资源
      最近更新 更多