【问题标题】:the operator '<' can't be unconditionally invoked because the receiver can be 'null'不能无条件调用运算符“<”,因为接收者可以为“null”
【发布时间】:2022-01-09 13:42:45
【问题描述】:

你能帮忙解决这个问题吗 我试过把 但没用

 (snapShot.hasData &&  snapShot.data! < 10)? '${snapShot.data}' ??'' : 'Future in Flutter'),

 (snapShot.hasData &&  snapShot!.data < 10)? '${snapShot.data}' ??'' : 'Future in Flutter'),

【问题讨论】:

  • 你到底想在 appBar 中显示什么?
  • 如果您的数据是 int,您可以解析为 int 或仅在 StreamBuilder 上添加类型,如果您正在检查长度,请包括该部分,同时确保处理 null 数据

标签: flutter dart


【解决方案1】:

请试试这个:

(snapShot.data ?? 0) < 10

【讨论】:

  • 没有解决,出现新问题Image Error Link
  • 将类型参数添加到您的 StreamBuilder -> StreamBuilder&lt;int&gt;,您得到错误的原因是没有类型参数,编译器不知道 snapShot.data 的类型。
  • 谢谢,已解决
  • 不客气 :)
【解决方案2】:

解决了问题


void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  info(String a) {
    return a;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: StreamBuilder<int>(
        stream: Stream.periodic(Duration(seconds: 1) , (a)=> a) ,
        builder: (context, snapShot) {
          return Scaffold(
            appBar: AppBar(
              title: Text(
                  (snapShot.hasData &&  (snapShot.data ?? 0) < 10)? '${snapShot.data}' : 'Future in Flutter'),
            ),
            body: Center(
              child: snapShot.connectionState == ConnectionState.waiting
                  ? const CircularProgressIndicator()
                  : const Text('Done!'),
            ),
          );
        },
      ),
    );
  }
}```

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 2022-01-12
    • 2021-07-03
    • 2021-06-02
    • 2023-03-05
    • 1970-01-01
    • 2022-08-24
    相关资源
    最近更新 更多