繁体   English   中英

Flutter Firestore 读写响应在 Ios 中非常慢

[英]Flutter Firestore Read Write Response Very Slow in Ios

我添加了 flutter 火库我的 ios 应用程序。 对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() 语句中的读取和写入中获得响应,但是需要 2-3 秒才能获得响应,有时我会在日志中收到此错误消息

控制台响应

有时也会收到此回复

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 设备中运行应用程序时,问题就出现了

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

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

暂无
暂无

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

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