-
-
Notifications
You must be signed in to change notification settings - Fork 307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@observable setter doesn't notify if inner ObservableList is changed #997
Comments
That's weird, I set up a minimal reproducible example, and everything works OK: // ignore_for_file: public_member_api_docs, sort_constructors_first
import 'package:flutter/foundation.dart';
import 'package:mobx/mobx.dart';
class User {
const User({
required this.name,
this.groups,
});
final String name;
final Groups? groups;
User copyWith({
String? name,
Groups? groups,
}) {
return User(
name: name ?? this.name,
groups: groups ?? this.groups,
);
}
@override
String toString() => 'User(name: $name, groups: $groups)';
@override
bool operator ==(covariant User other) {
if (identical(this, other)) return true;
return other.groups == groups;
}
@override
int get hashCode => Object.hash(name, groups);;
}
class Groups {
const Groups({
required this.users,
});
final ObservableList<User> users;
Groups copyWith({
ObservableList<User>? users,
}) {
return Groups(
users: users ?? this.users,
);
}
@override
String toString() => 'Groups(users: $users)';
@override
bool operator ==(covariant Groups other) {
if (identical(this, other)) return true;
return listEquals(other.users, users);
}
@override
int get hashCode => users.hashCode;
} import 'package:mobx/mobx.dart';
import '../models/user.dart';
part 'user_store.g.dart';
class UserStore = _UserStoreBase with _$UserStore;
abstract class _UserStoreBase with Store {
@observable
User? user;
@action
void init() {
user = User(
groups: Groups(
users: <User>[].asObservable(),
),
name: 'Max',
);
}
@action
void refreshUser() {
var newUser = user?.copyWith(
groups: Groups(
users: [const User(name: 'John Doe')].asObservable(),
),
);
user = newUser;
}
} import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:mobx_inner_observablelist_bug/stores/user_store.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late final UserStore userStore;
@override
void initState() {
super.initState();
userStore = UserStore()..init();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text('MobX bug'),
),
body: Center(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 24),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Observer(builder: (_) {
final userToString = userStore.user?.toString() ?? '';
return Text(userToString);
}),
const SizedBox(height: 24),
Observer(builder: (_) {
final familyUsers = userStore.user?.groups?.users;
return Text(familyUsers.toString());
})
],
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
userStore.refreshUser();
},
tooltip: 'Refresh',
child: const Icon(Icons.refresh),
),
);
}
}
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Seems like it's not fixed for @observable members.
Store:
User and UserGroups class:
Then I set a new User instance (with new users in group):
Observer doesn't react:
I also tried to check this behaviour and set it manually:
user = user.copyWith(...) -- Observer doesn't react
While directly changing ObservableList is working:
The text was updated successfully, but these errors were encountered: