【问题标题】:'package:flutter/src/widget/text.dart': Fasiled assertion: line 378 pos 10; 'data != null': A non-null String must be provided to a Text widget'package:flutter/src/widget/text.dart':断言失败:第 378 行 pos 10; 'data != null':必须向 Text 小部件提供非空字符串
【发布时间】:2023-03-24 14:37:01
【问题描述】:

手机访问record_patien时出现红屏

这是代码

import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/material.dart';
import 'package:sensor_detak_jantung/models/sensor.dart';
import 'package:sensor_detak_jantung/models/user.dart';
import 'package:sensor_detak_jantung/screens/authenticate/sign_in.dart';
import 'package:firebase_auth/firebase_auth.dart' as firebase_auth;
import 'package:sensor_detak_jantung/services/db_path.dart';

class RecordPatient extends StatefulWidget {
  const RecordPatient({Key key}) : super(key: key);

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

class _RecordPatient extends State<RecordPatient> {

  final databaseReference = FirebaseDatabase.instance.reference();
  final _auth = firebase_auth.FirebaseAuth.instance;
  final TextEditingController tokenController = TextEditingController();
  User userInfo;
  firebase_auth.User _user;
  Sensor sensorInfo;
  final _formKey = GlobalKey<FormState>();
  String bpm;
  void initState(){
    super.initState();
    this._user = _auth.currentUser;
    if(_user != null) {

  

这是我尝试获取 BPM 值的地方

      databaseReference.child('Sensor').once().then((DataSnapshot snapshot) {
        bpm = snapshot.value['BPM']['Data'];
      });

这是给用户的,这段代码在其他类上运行流畅。

      databaseReference.child(USER_KEY).child(_user.uid).once().then((snapshot) {
        userInfo = User.fromSnapshot(snapshot);
        setState(() { });
      });

    }
  }

  AppBar title(){
    return AppBar(
      backgroundColor: Colors.red[400],
      elevation: 0.0,
      title: Text('Hi ${userInfo?.userName}'),
      actions: <Widget>[
        FlatButton.icon(
          icon: Icon(Icons.person),
          label: Text('logout'),
          onPressed: () async {
            _auth.signOut().then((value) {
              Navigator.pushReplacement(context, new MaterialPageRoute(builder: (context) => SignIn()));
              setState(() {});
            });
          },
        ),
      ],
    );
  }

  @override


  Widget build(BuildContext context) {
    print (bpm);
    return Scaffold(
      body: Form(
        key: _formKey,
        child: ListView(
          padding: const EdgeInsets.fromLTRB(22.0, 0.0, 22.0, 22.0),
          children: [
            SizedBox(height: 40),
            title(),
            SizedBox(height: 40),
            Text (bpm),
          ],
        ),
      ),
    );
  }
}

当我尝试获取 Sensor 值和 users 值时会发生这种情况。

我仍在学习和试验,如果有人可以解释为什么这个错误会以简单的方式发生,非常感谢。

【问题讨论】:

    标签: firebase flutter dart


    【解决方案1】:

    正如错误中所写,Text 小部件得到一个空值,当我们将这样的内容传递给文本 Text(null) 时,这是一个错误。 在您的代码中,这仅适用于 bpm 可能为空的文本小部件。

    Text (bpm),
    

    所以你可以简单地做到这一点,

    databaseReference.child('Sensor').once().then((DataSnapshot snapshot) {
            bpm = snapshot.value['BPM']['Data']??"";
          });
    

    修复错误。

    【讨论】:

    • 我相信它与我最初用来获取 bpm 的代码相同。但你为什么放??到底?因为我的大部分代码都是来自不同教程的一部分。所以我试着学习它。
    • 我在分配 bpm 的第二行添加了空检查,因此它不能为空。 ??是空检查运算符,它检查是否为空,如果是,则简单地返回之后写入的内容,如本例中的“”。
    • 啊,我明白了,每次数据库更改时是否可以在不刷新屏幕的情况下更新 bpm 的值
    • 您可以使用 StreamBuilder 而不是 FutureBuilder。 Firebase 还提供了流,您可以搜索更多关于它们的信息。
    【解决方案2】:

    似乎snapshot.value['BPM']['Data']; 返回null,因此您的Text() 小部件将null 作为其值,这是不允许的。您应该在将 bpm 的值分配给 Text 小部件之前添加一个空检查,可能是这样的:

    SizedBox(height: 40),
    title(),
    SizedBox(height: 40),
    (bpm == null)?Text ("No BPM received"):Text(bpm),
    

    【讨论】:

      猜你喜欢
      • 2022-01-23
      • 2020-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-26
      • 2020-11-21
      • 2019-12-09
      • 2020-11-18
      相关资源
      最近更新 更多