我正在使用 Riverpod 提供程序类来处理从图库中选取图像。但是,一旦选择了图像,我就会收到错误:PlatformException(multiple_request,由第二个请求取消 null,null)。不确定第二个请求来自哪里。更重要的是,由于这种未知的取消,没有图像应用于我的占位符(CircleAvartar)。这是有问题的两个 dart 文件,感谢您的帮助。
图像提供者文件:
final myImageProvider =
ChangeNotifierProvider<ImageNotifier>((ref) => ImageNotifier());
class ImageNotifier extends ChangeNotifier {
ImageNotifier() : super();
final file = useState<File?>(null);
final imageFile = useState<XFile?>(null);
final imagePicker = ImagePicker();
Future<void> _pickImage(int type) async {
try {
XFile? userImage = await imagePicker.pickImage(
source: type == 1 ? ImageSource.gallery : ImageSource.camera,
imageQuality: 50,
);
imageFile.value = userImage;
// imageFile.value = XFile(userImage!.path);
} catch (e) {
print(e);
}
notifyListeners();
}
void showPicker(context) {
showModalBottomSheet(
backgroundColor: Theme.of(context).primaryColor,
context: context,
builder: …Run Code Online (Sandbox Code Playgroud) 我在flutter中使用来自ChangeNotifier的notifyListeners。notifyListeners()如果我连续拨打 3 次会怎样?在这种情况下,UI 会更新多少次?
notifyListeners\xd1\x81全部代码:
class Data with ChangeNotifier\n{\n String _data = 'some text';\n String get getData => _data;\n void changeString(String newString)\n {\n _data = newString;\n notifyListeners();\n notifyListeners();\n notifyListeners();\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n用法:
\nclass MyTextField extends StatelessWidget {\n const MyTextField({Key? key}) : super(key: key);\n @override\n Widget build(BuildContext context) {\n return TextField(\n onChanged: (newData) => context.read<Data>().changeString(newData),\n );\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n小部件树(如果很重要):
\nMyTextField是 的孩子Widget2。
Widget2是 的孩子Widget1。
Widget1是 的孩子 …