【问题标题】:Flutter Firestore Read Write Response Very Slow in IosFlutter Firestore 读写响应在 Ios 中非常慢
【发布时间】:2021-07-11 05:47:47
【问题描述】:

我已在我的 ios 应用程序中添加了 Flutter Firestore。对firestore的读写更新删除请求和往常一样很快,但是当我想从firestore服务器获取读写更新删除的响应时,它很慢,有时会失败。

我的 main.dart 中的读写代码是

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

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

final firestoreInstance = FirebaseFirestore.instance;
final auth = FirebaseAuth.instance;

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Future<void> adduser() async {
    await firestoreInstance.collection("users").add({
      "mob": 9344122678292,
      "email": "usehhr5",
      "name": {"fname": "Sub", "lname": "Collec"},
      "array": [
        "raw",
        "pin",
        "hot",
      ],
      "time": FieldValue.serverTimestamp(),
    }).then((value) {
      
      print("added_data");
      
    }).catchError((error) => print("Failed to add user: $error"));
  }

  Future<void> readdata() async {
    await firestoreInstance.collection("users").get().then((querySnapshot) {
      //firestoreInstance.clearPersistence();
      querySnapshot.docs.forEach((result) {
        print(result.data());

        print("read_data");
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          //adduser();
          readdata();
        },
        tooltip: 'Read/Add data',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

所以我想从 print() 语句中的 read 和 write 中获取响应,但是需要 2-3 秒才能获得响应,有时我会在日志中收到此错误消息

console response

有时也会收到此回复

7.3.0 - [Firebase/Firestore][I-FST000001] Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds.
 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.

还有这个回复

[BackgroundTask] Background Task 22 ("GTMSessionFetcher-www.googleapis.com"), was created over 30 seconds ago. In applications running in the background, this creates a risk of termination. Remember to call UIApplication.endBackgroundTask(_:) for your task in a timely manner to avoid this.

请提供帮助,我尝试了 ios 模拟器和物理 android 设备中 firestore 的读写响应,非常完美。我从 firesotre 服务器获得了即时响应。当我在运行 ios 14.4 的物理 ios 设备中运行应用程序时,问题就出现了

【问题讨论】:

    标签: ios firebase flutter google-cloud-firestore


    【解决方案1】:

    看来,您是直接从 StatefulWidget 调用 async 方法,这不好。那些 async 方法与 Firestore 交互,任何缓慢都会直接影响用户体验。

    一些建议:
    直接从小部件访问 Firestore 不是正确的方法。这将使您的应用程序无响应。 小部件应负责用户交互并做出响应。
    数据处理(Firestore 等)应由BLoC 处理并以async 方式完成。状态管理在构建 Flutter 应用中起着至关重要的作用。

    【讨论】:

      猜你喜欢
      • 2020-04-27
      • 1970-01-01
      • 2014-08-07
      • 1970-01-01
      • 1970-01-01
      • 2021-07-04
      • 2020-01-10
      • 1970-01-01
      • 2011-07-22
      相关资源
      最近更新 更多