Flutter RangeError(索引):无效值:有效值范围为空:0错误
你能告诉我我的代码有什么问题,以及如何避免在我的应用程序中显示红屏错误吗 ════════ widgets库捕获到异常═══════════════════════════════════ 生成聊天室消息(脏)时引发以下RangeError: RangeError(索引):无效值:有效值范围为空:0 我的代码:Flutter RangeError(索引):无效值:有效值范围为空:0错误,flutter,range,Flutter,Range,你能告诉我我的代码有什么问题,以及如何避免在我的应用程序中显示红屏错误吗 ════════ widgets库捕获到异常═══════════════════════════════════ 生成聊天室消息(脏)时引发以下RangeError: RangeError(索引):无效值:有效值范围为空:0 我的代码: import 'package:crypto_app/models/users.dart'; import 'package:crypto_app/pages/login.dart';
import 'package:crypto_app/models/users.dart';
import 'package:crypto_app/pages/login.dart';
import 'package:crypto_app/services/firestoreservice.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:meta/meta.dart';
void main() {
runApp(
FriendlyChatApp(),
);
}
final ThemeData kIOSTheme = ThemeData(
primarySwatch: Colors.blue,
primaryColor: Colors.grey[100],
primaryColorBrightness: Brightness.light,
);
final ThemeData kDefaultTheme = ThemeData(
primarySwatch: Colors.orange,
accentColor: Colors.orangeAccent,
);
String _name = '';
class FriendlyChatApp extends StatelessWidget {
const FriendlyChatApp({
Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: ChatScreen(),
);
}
}
class ChatMessage extends StatelessWidget {
ChatMessage({this.text, this.animationController});
final String text;
final AnimationController animationController;
@override
Widget build(BuildContext context) {
return SizeTransition(
sizeFactor:
CurvedAnimation(parent: animationController, curve: Curves.easeOut),
axisAlignment: 0.0,
child: Container(
margin: EdgeInsets.symmetric(vertical: 10.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
margin: const EdgeInsets.only(right: 16.0),
child: CircleAvatar(child: Text(_name[0])),
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(_name, style: Theme.of(context).textTheme.headline4),
Container(
margin: EdgeInsets.only(top: 5.0),
child: Text(text),
),
],
),
),
],
),
),
);
}
}
class ChatScreen extends StatefulWidget {
@override
_ChatScreenState createState() => _ChatScreenState();
}
class _ChatScreenState extends State<ChatScreen> with TickerProviderStateMixin {
final List<ChatMessage> _messages = [];
final _textController = TextEditingController();
final FocusNode _focusNode = FocusNode();
bool _isComposing = false;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
decoration: Theme.of(context).platform == TargetPlatform.iOS //new
? BoxDecoration(
border: Border(
top: BorderSide(color: Colors.grey[200]),
),
)
: null,
child: Column(
children: [
Flexible(
child: ListView.builder(
padding: EdgeInsets.all(8.0),
reverse: true,
itemBuilder: (_, int index) => _messages[index],
itemCount: _messages.length,
),
),
Divider(height: 1.0),
Container(
decoration: BoxDecoration(color: Theme.of(context).cardColor),
child: _buildTextComposer(),
),
],
),
),
);
}
Widget _buildTextComposer() {
return IconTheme(
data: IconThemeData(color: Theme.of(context).accentColor),
child: Container(
margin: EdgeInsets.symmetric(horizontal: 8.0),
child: Row(
children: [
Flexible(
child: TextField(
controller: _textController,
onChanged: (String text) {
setState(() {
_isComposing = text.isNotEmpty;
});
},
onSubmitted: _isComposing ? _handleSubmitted : null,
decoration: InputDecoration.collapsed(
hintText: 'Mesajınızı Buraya Yazınız:'),
focusNode: _focusNode,
),
),
Container(
margin: EdgeInsets.symmetric(horizontal: 4.0),
child: Theme.of(context).platform == TargetPlatform.iOS
? CupertinoButton(
onPressed: _isComposing
? () => _handleSubmitted(_textController.text)
: null,
child: Text('Gönder'),
)
: IconButton(
icon: const Icon(Icons.send),
onPressed: _isComposing
? () => _handleSubmitted(_textController.text)
: null,
))
],
),
),
);
}
void _handleSubmitted(String text) {
_textController.clear();
setState(() {
_isComposing = false;
});
var message = ChatMessage(
text: text,
animationController: AnimationController(
duration: const Duration(milliseconds: 700),
vsync: this,
),
);
setState(() {
_messages.insert(0, message);
});
_focusNode.requestFocus();
message.animationController.forward();
}
@override
void dispose() {
for (var message in _messages) {
message.animationController.dispose();
}
super.dispose();
}
}
import'包:crypto_应用程序/models/users.dart';
导入“package:crypto_app/pages/login.dart”;
导入“包:crypto_应用程序/services/firestoreservice.dart”;
导入“包:firebase_auth/firebase_auth.dart”;
进口“包装:颤振/cupertino.dart”;
进口“包装:颤振/基础.dart”;
进口“包装:颤振/材料.省道”;
导入“package:meta/meta.dart”;
void main(){
runApp(
FriendlyChatApp(),
);
}
最终主题数据kIOSTheme=主题数据(
主样本:颜色。蓝色,
原色:颜色。灰色[100],
primaryColorBrightness:Brightness.light,
);
最终主题数据kDefaultTheme=主题数据(
原始样本:颜色。橙色,
accentColor:Colors.orangeAccent,
);
字符串_name='';
类友好型ChatApp扩展了无状态小部件{
const FriendlyChatApp({
关键点,
}):super(key:key);
@凌驾
小部件构建(构建上下文){
返回材料PP(
debugShowCheckedModeBanner:false,
主页:ChatScreen(),
);
}
}
类ChatMessage扩展了无状态小部件{
ChatMessage({this.text,this.animationController});
最终字符串文本;
最终AnimationController AnimationController;
@凌驾
小部件构建(构建上下文){
返回大小转换(
尺寸因子:
曲线动画(父对象:animationController,曲线:Curves.easeOut),
axisAlignment:0.0,
子:容器(
边缘:边缘组。对称(垂直:10.0),
孩子:排(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
容器(
边距:仅限常量边集(右:16.0),
child:CircleAvatar(child:Text(_name[0])),
),
扩大(
子:列(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
文本(_name,style:Theme.of(context.textTheme.headline4),
容器(
边距:仅限边集(顶部:5.0),
子:文本(Text),
),
],
),
),
],
),
),
);
}
}
类ChatScreen扩展StatefulWidget{
@凌驾
_ChatScreenState createState()=>\u ChatScreenState();
}
类\u ChatScreenState使用TickerProviderStateMixin扩展状态{
最终列表_消息=[];
final _textController=TextEditingController();
最终焦点节点_FocusNode=FocusNode();
bool _isComposing=false;
@凌驾
小部件构建(构建上下文){
返回脚手架(
主体:容器(
装饰:Theme.of(context.platform==TargetPlatform.iOS//new
?盒子装饰(
边界:边界(
顶部:边框侧(颜色:Colors.grey[200]),
),
)
:null,
子:列(
儿童:[
灵活的(
子项:ListView.builder(
填充:边缘设置。全部(8.0),
相反:是的,
itemBuilder:(\u,int index)=>\u消息[索引],
itemCount:_messages.length,
),
),
分隔器(高度:1.0),
容器(
装饰:盒子装饰(颜色:主题.of(上下文).cardColor),
子项:_buildTextComposer(),
),
],
),
),
);
}
小部件_buildTextComposer(){
返回象素(
数据:IconThemeData(颜色:Theme.of(context).accentColor),
子:容器(
边缘:边缘组。对称(水平:8.0),
孩子:排(
儿童:[
灵活的(
孩子:TextField(
控制器:_textController,
onChanged:(字符串文本){
设置状态(){
_isComposing=text.isNotEmpty;
});
},
onSubmitted:_isComposing?_handleSubmitted:null,
装饰:输入装饰(
hintText:“MesajınızıBuraya Yazınız:”),
focusNode:_focusNode,
),
),
容器(
边缘:边缘组。对称(水平:4.0),
child:Theme.of(context.platform==TargetPlatform.iOS
?CupertinoButton(
onPressed:\u正在编写
?()=>_handleSubmitted(_textController.text)
:null,
child:Text('Gönder'),
)
:图标按钮(
图标:常量图标(Icons.send),
onPressed:\u正在编写
?()=>_handleSubmitted(_textController.text)
:null,
))
],
),
),
);
}
void\u handleSubmitted(字符串文本){
_textController.clear();
设置状态(){
_isComposing=false;
});
var message=ChatMessage(
文本:文本,
animationController:animationController(
持续时间:常量持续时间(毫秒:700),
vsync:这个,,
),
);
设置状态(){
_消息。插入(0,消息);
});
_focusNode.requestFocus();
message.animationController.forward();
}
@凌驾
无效处置(){
for(变量消息在_消息中){
message.animationController.dispose();
}
super.dispose();
}
}
<
CircleAvatar(child: Text(_name[0])),
String _name = '';
String _name = 'Random';