Flutter Mobx Observer 不重建
Flutter Mobx Observer doesnt rebuild
我没有想法了。
我使用 Mobx 进行非常简单的状态管理。
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:jw_helper/state/globalState.dart';
class Router extends StatelessWidget {
const Router({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
final _globalState = GlobalState();
return Column(
children: <Widget>[
Container(
child: Observer(
builder: (_) => Text(_globalState?.currentIndex?.toString()),
),
),
MaterialButton(
onPressed: () {
_globalState.setCurrentIndex(1);
},
child: Text("Press me"),
),
],
);
}
}
当我更改此小部件中的状态时,值会更新。
当我在另一个 Widget 中改变同一个 Observable 时,Observer 没有重建。
仅更新状态发生变化的同一个Widget中的观察者。
我的 Mobx 代码:
import 'package:mobx/mobx.dart';
// Include generated file
part 'globalState.g.dart';
// This is the class used by rest of your codebase
class GlobalState = _GlobalState with _$GlobalState;
// The store-class
abstract class _GlobalState with Store {
@observable
int currentIndex = 0;
@action
void setCurrentIndex(index) {
currentIndex = index;
print(currentIndex);
}
}
小提示:打印语句总是被触发
也许有人知道如何解决这个问题。
谢谢 ;)
问题已在 Discord Mobx 频道成员的帮助下解决。
解决方案是将整个应用程序包装在提供者小部件中。
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
------------------------------------------------
return Provider<GlobalState>(
create: (context) => GlobalState(),
------------------------------------------------
child: MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: SplashScreen(),
),
);
}
}
在使用 Mobx 的小部件中 Class 我做了:
class Router extends StatelessWidget {
const Router({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
final _globalState = Provider.of<GlobalState>(context);
return Column(
children: <Widget>[
Container(.....
希望这有助于有人起床 运行 ;)
不是一个完成的(也不会是)应用程序,但希望可以作为一个开始。
Flutter + Mobx +(多)提供商
您有两个 GlobalState 实例 class。每个小部件一个。为了让 Observer 正常工作,它需要始终观察同一个实例。
使用“Provider”你有点像使用解决问题的单例模式,因为两个变量开始引用同一个实例
有同样的问题
在主存储文件中进行任何更改后始终重建 mobX
flutter pub 运行 build_runner build --delete-conflicting-outputs
我没有想法了。
我使用 Mobx 进行非常简单的状态管理。
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:jw_helper/state/globalState.dart';
class Router extends StatelessWidget {
const Router({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
final _globalState = GlobalState();
return Column(
children: <Widget>[
Container(
child: Observer(
builder: (_) => Text(_globalState?.currentIndex?.toString()),
),
),
MaterialButton(
onPressed: () {
_globalState.setCurrentIndex(1);
},
child: Text("Press me"),
),
],
);
}
}
当我更改此小部件中的状态时,值会更新。 当我在另一个 Widget 中改变同一个 Observable 时,Observer 没有重建。
仅更新状态发生变化的同一个Widget中的观察者。
我的 Mobx 代码:
import 'package:mobx/mobx.dart';
// Include generated file
part 'globalState.g.dart';
// This is the class used by rest of your codebase
class GlobalState = _GlobalState with _$GlobalState;
// The store-class
abstract class _GlobalState with Store {
@observable
int currentIndex = 0;
@action
void setCurrentIndex(index) {
currentIndex = index;
print(currentIndex);
}
}
小提示:打印语句总是被触发
也许有人知道如何解决这个问题。 谢谢 ;)
问题已在 Discord Mobx 频道成员的帮助下解决。
解决方案是将整个应用程序包装在提供者小部件中。
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
------------------------------------------------
return Provider<GlobalState>(
create: (context) => GlobalState(),
------------------------------------------------
child: MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: SplashScreen(),
),
);
}
}
在使用 Mobx 的小部件中 Class 我做了:
class Router extends StatelessWidget {
const Router({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
final _globalState = Provider.of<GlobalState>(context);
return Column(
children: <Widget>[
Container(.....
希望这有助于有人起床 运行 ;)
不是一个完成的(也不会是)应用程序,但希望可以作为一个开始。
Flutter + Mobx +(多)提供商
您有两个 GlobalState 实例 class。每个小部件一个。为了让 Observer 正常工作,它需要始终观察同一个实例。
使用“Provider”你有点像使用解决问题的单例模式,因为两个变量开始引用同一个实例
有同样的问题
在主存储文件中进行任何更改后始终重建 mobX
flutter pub 运行 build_runner build --delete-conflicting-outputs