我正在尝试为我的应用程序实现自定义注销解决方案,无论用户当前在哪里,一旦Logout button点击,应用程序将导航回Login page.
我的想法是,与其监听每个组件的状态更改,不如在主组件上使用一个监听器 -> MyApp。
为简单起见,我将项目精简到最低限度。这是我的 Profile 类的样子:
class Profile with ChangeNotifier {
bool _isAuthentificated = false;
bool get isAuthentificated => _isAuthentificated;
set isAuthentificated(bool newVal) {
_isAuthentificated = newVal;
notifyListeners();
}
}
Run Code Online (Sandbox Code Playgroud)
现在,在 下Main,我已注册此提供程序如下:
void main() => runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(
create: (_) => Profile(),
)
],
child: MyApp(),
),
);
Run Code Online (Sandbox Code Playgroud)
最后是MyApp组件:
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext …Run Code Online (Sandbox Code Playgroud) 我知道颤振中的状态管理存在某些问题,但它们似乎都不能解决我的用例。
最近我正在构建一个基于电子商务的应用程序,并遇到了这种对状态管理的真正需求。我有2页,
用户可以将产品添加到购物车,然后导航到购物车。在购物车页面中,他们还可以增加或减少商品的数量。
正如您所看到的,最初黄色商品被添加到购物车中,数量为 3,但在购物车页面中,用户按下 (-) 按钮,将数量减少到 1。现在,如果用户按下后退按钮,则状态为黄色产品在产品页面上仍为 3。与粉色产品类似的情况。
因此,我尝试使用正常的 setState 和一些全局数据结构来解决这个问题来跟踪数量。但随着我的应用程序变得越来越大,跨页面维护状态真的很困难。我读过不同的状态管理解决方案,例如 redux、BloC 模式、Provider,...但我不清楚如何在我的用例中使用它们。有没有有经验的人帮我把石头敲碎,帮我消化一下?
任何有关如何解决此问题的示例代码和解释将不胜感激!
希望我的问题是清楚的!
state-management flutter flutter-provider flutter-bloc flutter-state
我是 Dart 语言开发的初学者。我尝试创建一个受此GitHub repo启发的示例 Flutter 应用程序 BLOC 模式,但我收到了一些与类继承相关的错误消息。我已经熟悉点网C#语言中的继承和超类和子类编程。但就飞镖而言,我需要一些建议。
这是我的代码:
class UserRegBloc extends Bloc<UserRegEvent, UserRegState> {
UserRepository userRepository;
UserRegBloc({@required UserRepository userRepository}) {
userRepository = UserRepository();
}
@override
UserRegState get initialState => UserRegInitial();
@override
Stream<UserRegState> mapEventToState(UserRegEvent event) async* {
if (event is SignUpButtonPressed) {
yield UserRegLoading();
try {
var user = await userRepository.signUpUserWithEmailPass(
event.email, event.password);
print("BLOC : ${user.email}");
yield UserRegSuccessful(user: user);
} catch (e) {
yield UserRegFailure(message: e.toString());
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的 pubspec.yaml 依赖项如下:
当 的 类型为 时, Flutter Riverpod 不会通知Consumer状态变化,而相同的实现对于其他类型来说效果很好。StateNotifierList
在这里,我提供了一个最小的可重现示例:
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ProviderScope(
child: MaterialApp(
home: MyHomePage(),
),
);
}
}
class CounterState extends StateNotifier<List<int>> {
static final provider = StateProvider(
(ref) => CounterState(),
);
int get last {
print('last');
return state.last;
}
int get length {
print('len');
return state.length;
}
// the body of this will be …Run Code Online (Sandbox Code Playgroud) 当我的应用程序以发布模式启动时,我遇到了一个相当奇怪的错误:
我按照这个PageView答案使用和保留我的 BottomNavigationBar 的页面AutomaticKeepAliveClientMixin。在调试模式下,不会发生此错误。
home.dart
import 'package:flutter/material.dart';
import 'package:lottery_rewards/controller.dart';
class HomeView extends StatefulWidget {
const HomeView({Key? key}) : super(key: key);
@override
State<StatefulWidget> createState() => _HomeView();
}
class _HomeView extends State<HomeView>
with AutomaticKeepAliveClientMixin<HomeView> {
@override
Widget build(BuildContext context) {
super.build(context);
return Column(
children: [
const MainAppBar(),
Expanded(
child: Center(
child: TextButton(
child: const Text('Home'),
onPressed: () {
},
),
),
)
],
);
}
@override
bool get wantKeepAlive => true;
}
Run Code Online (Sandbox Code Playgroud)
同样在终端中,它显示“对空值使用空检查运算符”,但是我不知道在哪里使用了这段代码。
I/flutter (29449): Null check …Run Code Online (Sandbox Code Playgroud) 我正在使用 Navigator 2.0,并且我将状态存储在其中,RouterDelegate以便我可以currentConfiguration对状态更改做出反应。
这就像我看到的所有例子一样。例如:
BookRoutePath get currentConfiguration {
return _selectedBook == null
? BookRoutePath.home()
: BookRoutePath.details(books.indexOf(_selectedBook));
}
Run Code Online (Sandbox Code Playgroud)
如何设置_selectedBook树下某个小部件的属性?我在应用程序的其余部分使用Provider并传递ChangeNotifier.
但是我不知道如何做到这一点RouterDelegate。
有什么办法可以访问它的状态吗?
或者有没有一种方法可以将状态存储在另一个类中,并且currentConfiguration每次_selectedBook更改时仍然可以更新?
Im trying to implement some basic state management in my Flutter app. Somewhere up the Widget tree I have a Provider for a User. Widgets further down can access the User using the Consumer User Widget. However one Widget (WidgetB in the code snipped below) is build using the Navigator.push() and cant access the User. Pushing the Button will throw the Error:
Error: Could not find the correct Provider above this Consumer Widget
floatingActionButton: FloatingActionButton(
child: Icon(Icons.play),
onPressed: () { …Run Code Online (Sandbox Code Playgroud) 使用provider,Providers 的作用域位于小部件树中。只有小部件的子Provider级可以访问其模型。
它在 的 doc 中说riverpod:
允许在多个位置轻松访问该状态。提供者完全替代了单例、服务定位器、依赖注入或 InheritedWidgets 等模式。
* 这里的“ Providers”指的是
Provider包的类riverpod。
这个provider包是围绕 s 的简化/包装/API InheritedWidget,所以我想 s 可以实现的功能provider也可以通过 s 实现riverpod。
但我无法找出如何做到这一点。
这是我正在尝试迁移的一个小例子。
class Counter extends ValueNotifier<int> {
Counter(): super(0);
}
class MyWidget extends StatelessWidget {
const MyWidget();
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<Counter>(
create: (_) => Counter();
child: Builder(
builder: (context) {
return Row( …Run Code Online (Sandbox Code Playgroud) flutter ×10
flutter-state ×10
dart ×4
flutter-bloc ×2
riverpod ×2
android ×1
bloc ×1
flutter-getx ×1