繁体   English   中英

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

[英]unable to reach firebase backend with flutter app

我从 firebase 和 flutter 开始。 我想从工作代码开始,所以我实现了婴儿名字代码实验室。 有效。 然后停止工作。 我尝试更新 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 运行它以防万一。 然后我删除了该项目并将其放回东南亚。 这些都没有对这个错误产生任何影响。

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

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

暂无
暂无

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

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