Flutter 关于String isNotEmpty 判空

String

Dart 里的 String是不可变对象;要检测两个 String 的内容是否一样时,我们使用 == 进行比较;如果要测试两个对象是否是同一个对象(indentity test),使用 identical 函数。

示例

void _stringDeals() {
    var abc = ' abc ';

    ///带空格
    var abcUp = abc.toUpperCase();

    ///字符大写
    var abcTrim = abc.trim();

    ///去掉空格
    ///assert 是语言内置的断言函数,仅在检查模式下有效,如果断言失败则程序立刻终止
    ///判断内容是否相等
    assert(abc == abcUp);

    ///判断对象是否相等
    assert(!identical(abc, abcUp));
    
  }
  }

关键语句
Dart 里所有的东西都是对象,包括 int、函数。

判空问题

场景一

用String Api 的 length 函数 去判断

var value = "1234567";
if (value.length != 0) {
      print("value不为空");
    }
    

这样会遇到一个问题就是value 如果为Null 那么Flutter就红屏了 程序结束

var value = null;
程序出错结束

非常不推荐这么取判断

场景二

那么我们用String Api的 isNotEmpty 函数 去判断

var value = "1234567";
 if (value.isNotEmpty) {
      print("value不为空");
    }

这样我们又会碰到同样的问题就是如果value=null

var value = null;
程序抛出异常
Another exception was thrown: NoSuchMethodError: 
The getter 'isNotEmpty' was called on null.
出错点后面的程序都不会继续执行
场景三

我们用String != null 去判断

var value = "1234567";
 if (null !=value ) {
      print("value不为空");
    }

这样我们碰到value=null 或者 value有值就不会报错
这样的场景多发生在后端给返回的字符串String 是null 的情况

总结

结合实际出发 后端规范那就不需要去用!=null 来判断 ,但是不能用value.length函数来判断,这样非常有问题。保守就是使用null !=value

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Flutter中,可以使用自定义Widget来实现自定义键盘。以下是实现自定义键盘的一般步骤: 1. 创建一个StatefulWidget,该Widget将显示自定义键盘。 2. 实现一个键盘布局。可以使用Flutter提供的各种Widget(如Container、Row、Column等)来创建键盘布局,并使用GestureDetector Widget来处理键盘按钮的点击事件。 3. 在需要使用自定义键盘的地方,使用Flutter的FocusNode来控制输入框的焦点,并将自定义键盘与输入框关联起来。 下面是一个简单的实现自定义键盘的示例代码: ```dart import 'package:flutter/material.dart'; class CustomKeyboard extends StatefulWidget { @override _CustomKeyboardState createState() => _CustomKeyboardState(); } class _CustomKeyboardState extends State<CustomKeyboard> { TextEditingController _textEditingController = TextEditingController(); @override Widget build(BuildContext context) { return Container( child: Column( children: [ TextField( controller: _textEditingController, focusNode: FocusNode(), decoration: InputDecoration( hintText: 'Input something', ), ), Row( children: [ CustomButton('1'), CustomButton('2'), CustomButton('3'), ], ), Row( children: [ CustomButton('4'), CustomButton('5'), CustomButton('6'), ], ), Row( children: [ CustomButton('7'), CustomButton('8'), CustomButton('9'), ], ), Row( children: [ CustomButton('0'), CustomButton('backspace'), ], ), ], ), ); } } class CustomButton extends StatelessWidget { final String text; CustomButton(this.text); @override Widget build(BuildContext context) { return GestureDetector( onTap: () { if (text == 'backspace') { String currentText = CustomKeyboard.of(context) .controller .text; if (currentText.isNotEmpty) { CustomKeyboard.of(context) .controller .text = currentText.substring(0, currentText.length - 1); } } else { CustomKeyboard.of(context) .controller .text += text; } }, child: Container( width: 100.0, height: 80.0, decoration: BoxDecoration( border: Border.all( color: Colors.grey, ), ), child: Center( child: Text(text), ), ), ); } } class CustomKeyboardController { final TextEditingController controller; CustomKeyboardController(this.controller); void clear() { controller.clear(); } void close() { FocusScope.of(controller.context).requestFocus(FocusNode()); } } class CustomKeyboardScope extends InheritedWidget { final CustomKeyboardController controller; CustomKeyboardScope({ Key key, @required Widget child, }) : assert(child != null), controller = CustomKeyboardController(TextEditingController()), super(key: key, child: child); static CustomKeyboardController of(BuildContext context) { return (context.dependOnInheritedWidgetOfExactType<CustomKeyboardScope>()) .controller; } @override bool updateShouldNotify(CustomKeyboardScope oldWidget) { return controller != oldWidget.controller; } } ``` 在上面的示例代码中,`CustomKeyboard` Widget是自定义键盘的主Widget,它包含了一个`TextField`和一些自定义的按钮。 `CustomButton` Widget是自定义键盘中的按键,它使用`GestureDetector`来处理按钮点击事件。 `CustomKeyboardController`是自定义键盘的控制器,用于控制输入框的文本内容。 最后,使用`CustomKeyboardScope` Widget来将`CustomKeyboardController`注入到自定义键盘中。 在需要使用自定义键盘的地方,可以使用以下代码来显示自定义键盘: ```dart CustomKeyboardScope( child: CustomKeyboard(), ); ``` 同时,需要将`CustomKeyboardController`与输入框关联起来,可以使用以下代码: ```dart TextField( controller: CustomKeyboardScope.of(context).controller.textEditingController, focusNode: FocusNode(), ); ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值