Riverpod-更新StateNotifier的单个属性时重建屏幕



在使用Riverpod作为状态管理工具时,我一直在使用我发现的反模式。我做了提出此问题的用户所做的事情:更新要更改的属性并添加state = state。该解决方案一直有效,直到state_provider包更新到0.7.0之后,该解决方案才完全停止工作。

我一直在尝试下面线程中使用的解决方案,但这也不会为我重建UI。有人能告诉我我做错了什么吗?我觉得下面的解决方案应该有效,因为这基本上是你对任何其他状态管理包(如Redux(所做的。

我的状态模型、通知程序和提供商

import 'package:hooks_riverpod/hooks_riverpod.dart';
class Test {
Test({this.prop1, this.prop2});
String? prop1;
int? prop2;
}
class TestNotifier extends StateNotifier<Test> {
TestNotifier() : super(Test());
void update() {
state.prop2 ??= 0;
state = state..prop2 = state.prop2! + 1;
}
}
final StateNotifierProvider<TestNotifier, Test> testProvider =
StateNotifierProvider((ref) => TestNotifier());

完整的main.dart文件

import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:practice_buddy/state/test_state.dart';
void main() {
runApp(const ProviderScope(child: MyApp()));
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends HookConsumerWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
Widget build(BuildContext context, WidgetRef ref) {
final _testProvider = ref.watch(testProvider);
final _testNotifier = ref.watch(testProvider.notifier);
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'${_testProvider.prop2 ?? 0}',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () => _testNotifier.update(),
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}

Pubspec显示软件包版本

dependencies:
cloud_firestore: ^3.1.7
firebase_auth: ^3.3.6
firebase_core: ^1.12.0
flutter:
sdk: flutter
flutter_hooks: ^0.18.2
hooks_riverpod: ^1.0.3

我的Flutter版本是2.8.1。

这可以通过在更新时传递一个新实例来解决。

void update() {
state.prop2 ??= 0;
state = state.copyWith(prop2: state.prop2! + 1);
}

以及模型类copyWith构造函数。

Test copyWith({
String? prop1,
int? prop2,
}) {
return Test(
prop1: prop1 ?? this.prop1,
prop2: prop2 ?? this.prop2,
);
}

最新更新