我是Dart的新手,所以如果这个问题似乎微不足道或不需要,我深表歉意!
现在,我已经在全局范围内初始化了一个List<String>
变量,并通过同一类中的另一种方法向其中添加了值:
// global variable
List<String> images = List(2);
getImages() async {
// tried adding values but it is still null
images[0] = ("https://i.pinimg.com/236x/b5/03/dd/b503dd2350f4b5c248a1f0909433fcfe.jpg");
images[1] = ("https://i.pinimg.com/236x/04/80/ff/0480ff67b54dc03bb18da3d90e2c2a82.jpg");
// get a Future<List<String> from another class
List<String> items = await FirebaseStorage().getMoodBoardImages();
print("items: " + items[0]); // returns a value
print("images" + images[0]); // cause of error
images.addAll(items);
}
堆栈跟踪:
E/flutter (28986): [ERROR:flutter/shell/common/shell.cc(213)] Dart Error: Unhandled exception:
E/flutter (28986): NoSuchMethodError: The method '[]=' was called on null.
E/flutter (28986): Receiver: null
E/flutter (28986): Tried calling: []=(0, "https://i.pinimg.com/236x/b5/03/dd/b503dd2350f4b5c248a1f0909433fcfe.jpg")
E/flutter (28986): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
E/flutter (28986): #1 _ProfileState.getImages (package:calmante/screens/profile/profile.dart:334:11)
E/flutter (28986): #2 _ProfileState.build (package:calmante/screens/profile/profile.dart:30:5)
编辑:以下代码部分正确,因为已删除了异常,但我无法通过Flutter显示图像网址
getImages() async {
images = List(2); // Add this
// tried adding values but it is still null
images[0] = ("https://i.pinimg.com/236x/b5/03/dd/b503dd2350f4b5c248a1f0909433fcfe.jpg");
images[1] = ("https://i.pinimg.com/236x/04/80/ff/0480ff67b54dc03bb18da3d90e2c2a82.jpg");
// get a Future<List<String> from another class
List<String> items = await FirebaseStorage().getMoodBoardImages();
print("items: " + items[0]); // returns a value
print("images" + images[0]); // cause of error
images.addAll(items);
}
编辑:因此,我认为上述代码仅在发生异常的位置进行了更改,因为现在它是在build方法内部触发的。
代码:
@override
Widget build(BuildContext context) {
getImages();
setPalette();
print("images build: " + images[0]);
}
StackTrace:
The following ArgumentError was thrown building Profile(dirty, state: _ProfileState#71db6):
Invalid argument(s)
The relevant error-causing widget was:
Profile file:///C:/Users/ugam.UG-GW-L163/AndroidStudioProjects/calmante/lib/screens/first.dart:19:5
When the exception was thrown, this was the stack:
#0 _StringBase.+ (dart:core-patch/string_patch.dart:262:57)
#1 _ProfileState.build (package:calmante/screens/profile/profile.dart:33:28)
#2 StatefulElement.build (package:flutter/src/widgets/framework.dart:4619:28)
#3 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4502:15)
#4 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4675:11)
...
答案 0 :(得分:0)
可能无法满足您需求的快速答案:在images
上方重新初始化getImages()
:
getImages() async {
images = List(2); // Add this
// tried adding values but it is still null
images[0] = ("https://i.pinimg.com/236x/b5/03/dd/b503dd2350f4b5c248a1f0909433fcfe.jpg");
images[1] = ("https://i.pinimg.com/236x/04/80/ff/0480ff67b54dc03bb18da3d90e2c2a82.jpg");
// get a Future<List<String> from another class
List<String> items = await FirebaseStorage().getMoodBoardImages();
print("items: " + items[0]); // returns a value
print("images" + images[0]); // cause of error
images.addAll(items);
}