Flutter 错误:“Null”类型不是“String”类型的子类型
Posted
技术标签:
【中文标题】Flutter 错误:“Null”类型不是“String”类型的子类型【英文标题】:Flutter Error: type 'Null' is not a subtype of type 'String' 【发布时间】:2021-09-01 06:13:44 【问题描述】:我是 Flutter 的新手。我正在尝试为 ios 或 android 开发银行应用程序项目。我不明白这个错误是什么意思。我看不出有什么可以对代码进行更改。而且,当我编译时,我得到了这个错误:
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following _TypeError was thrown building HomeScreen(dirty, state: _HomeScreenState#e34f3):
type 'Null' is not a subtype of type 'String'
The relevant error-causing widget was:
HomeScreen file:///Users/berfin/VSCodeProjects/bankingapp/lib/main.dart:14:13
When the exception was thrown, this was the stack:
#0 operation.<anonymous closure> (package:bankingapp/models/operation.dart:12:51)
#1 MappedListIterable.elementAt (dart:_internal/iterable.dart:412:31)
#2 ListIterator.moveNext (dart:_internal/iterable.dart:341:26)
#3 new _GrowableList._ofEfficientLengthIterable (dart:core-patch/growable_array.dart:188:27)
#4 new _GrowableList.of (dart:core-patch/growable_array.dart:150:28)
#5 new List.of (dart:core-patch/array_patch.dart:50:28)
#6 ListIterable.toList (dart:_internal/iterable.dart:212:44)
#7 operation (package:bankingapp/models/operation.dart:14:6)
#8 operation (package:bankingapp/models/operation.dart)
#9 _HomeScreenState.build (package:bankingapp/screens/homescreen.dart:189:49)
#10 StatefulElement.build (package:flutter/src/widgets/framework.dart:4691:27)
#11 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4574:15)
#12 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4746:11)
#13 Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5)
#14 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4553:5)
#15 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4737:11)
#16 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4548:5)
... Normal element mounting (24 frames)
#40 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14)
#41 MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6221:36)
#42 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6232:32)
... Normal element mounting (170 frames)
#212 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14)
#213 MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6221:36)
#214 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6232:32)
... Normal element mounting (345 frames)
#559 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14)
#560 Element.updateChild (package:flutter/src/widgets/framework.dart:3363:18)
#561 RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1189:16)
#562 RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1159:5)
#563 RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure>
(package:flutter/src/widgets/binding.dart:1104:18)
#564 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2535:19)
#565 RenderObjectToWidgetAdapter.attachToRenderTree
(package:flutter/src/widgets/binding.dart:1103:13)
#566 WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:937:7)
#567 WidgetsBinding.scheduleAttachRootWidget.<anonymous closure>
(package:flutter/src/widgets/binding.dart:917:7)
(elided 11 frames from class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch)
════════════════════════════════════════════════════════════════════════════════════════════════════
homescreen.dart
Padding(
padding: EdgeInsets.only(
left: 16, bottom: 13, top: 29, right: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
"Operation",
style: GoogleFonts.inter(
fontSize: 16,
fontWeight: FontWeight.w300,
color: kBlackColor),
),
Row(
children: map<Widget>(operation, (index, selected)
return Container(
alignment: Alignment.centerLeft,
height: 9,
width: 9,
margin: EdgeInsets.only(right: 6),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: current == index
? kBlueColor
: kTwentyBlueColor),
);
),
)
],
),
),
Container(
height: 123,
child: ListView.builder(
itemCount: operation.length,
padding: EdgeInsets.only(left: 16),
scrollDirection: Axis.horizontal,
itemBuilder: (context, index)
return GestureDetector(
onTap: ()
setState(()
current = index;
);
,
child: OperationCard(
operation: operation[index].name,
selectedItem: operation[index].selectedItem,
unselectedItem: operation[index].unselectedItem,
isSelected: current == index,
context: this),
);
),
)
operation.dart(模型)
class OperationModel
String name;
String selectedItem;
String unselectedItem;
OperationModel(this.name, this.selectedItem, this.unselectedItem);
List<OperationModel> operation = operationData
.map(
(item) => OperationModel(
item['name'], item['selectedItem'], item['unselectedItem']), // ERROR IS HERE
)
.toList();
List<Map<String, dynamic>> operationData = [
"name": "Money\nTransfer",
"selectedItem": "assets/svg/money_transfer_white.svg",
"unselectedItem": "assets/svg/money_transfer_blue.svg"
,
"name": "Bank\nWithdraw",
"selectedItem": "assets/svg/bank_withdraw_white.svg",
"unselectedIcon": "assets/svg/bank_withdraw_blue.svg"
,
"name": "Insight\nTracking",
"selectedItem": "assets/svg/insight_tracking_white.svg",
"unselectedItem": "assets/svg/insight_tracking_blue.svg"
,
];
操作卡(类)
class OperationCard extends StatefulWidget
final String operation;
final String selectedItem;
final String unselectedItem;
final bool isSelected;
_HomeScreenState context;
OperationCard(
required this.operation,
required this.selectedItem,
required this.unselectedItem,
required this.isSelected,
required this.context);
@override
_OperationCardState createState() => _OperationCardState();
class _OperationCardState extends State<OperationCard>
@override
Widget build(BuildContext context)
return Container(
margin: EdgeInsets.only(right: 16),
width: 123,
height: 123,
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: kTenBlackColor,
blurRadius: 10,
spreadRadius: 5,
offset: Offset(8.0, 8.0))
],
borderRadius: BorderRadius.circular(15),
color: widget.isSelected ? kBlueColor : kWhiteColor),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SvgPicture.asset(
widget.isSelected ? widget.selectedItem : widget.unselectedItem),
SizedBox(
height: 9,
),
Text(
widget.operation,
textAlign: TextAlign.center,
style: GoogleFonts.inter(
fontSize: 15,
fontWeight: FontWeight.w700,
color: widget.isSelected ? kBlueColor : kWhiteColor),
)
],
),
);
这个错误应该很快就会修复,因为我无法继续这个项目。请帮我!! :):):):)
【问题讨论】:
dart.dev/null-safety阅读这篇文章 您能否提及您在哪一行代码中遇到了错误?这将是 heloful 哦,好的。我正在编辑问题。 (错误在 operation.dart 内部) 【参考方案1】:试试这个:
OperationModel(
item['name']??"", item['selectedItem']?? "", item['unselectedItem']??""),
错误是你在String中存储了null,确保没有传递null,这就是我添加的原因?? ,该运算符检查变量是否为null,如果是,则将空字符串传递给“”。
【讨论】:
我做到了,但我收到了关于 SVG 图片的新错误。我在 IOS 模拟器中没有看到任何 svg 图片以上是关于Flutter 错误:“Null”类型不是“String”类型的子类型的主要内容,如果未能解决你的问题,请参考以下文章
Flutter 错误:_TypeError(类型“Null”不是“FutureOr<Database>”类型的子类型)
错误:flutter/lib/ui/ui_dart_state.cc(157) 未处理的异常:类型 'Future<dynamic>' 不是类型 'FutureOr<Null>
Flutter 未处理的异常:类型“Null”不是类型转换中“List<dynamic>”类型的子类型
Flutter 'showDatePicker' 抛出异常:错误:预期值为 'String' 类型,但得到类型为 'Null' 的值之一
Flutter:类型 'Future<dynamic>' 不是类型 'Widget' 的子类型 // MaterialPageRoute<dynamic>(null))