I would like to have the following logic on this page:
Fetch user with riverpod and then set the TextEditingControllers state once the page loads.
Then, in another page, I would like to again get the already set state (maybe not even fetch the user again?) and then be able to change the user info in the UI. Once I do that, I would like to save the info to the DB and then to reflect those changes in the Riverpod state when I am navigating back.
Hope this makes sense
class MyAccount extends ConsumerStatefulWidget {
const MyAccount({super.key});
@override
ConsumerState<MyAccount> createState() => _MyAccountState();
}
double gap = 10;
class _MyAccountState extends ConsumerState<MyAccount> {
TextEditingController dobController = TextEditingController();
TextEditingController heightController = TextEditingController();
TextEditingController ageController = TextEditingController();
int? _genderSelectedIndex;
int? _interestSelectedIndex;
int? _lookingForIndex;
List<String> _selectedHobbies = [];
double width = 200;
double gap = 10;
Future<List<UserClass>> getUser() async {
UserResponse user = await supabase.auth.getUser();
if (user.user?.id != null) {
List<Map<String, dynamic>> response =
await supabase.from('users').select().eq('id', user.user!.id);
return response.map((data) => UserClass.fromMap(data)).toList();
}
return [];
}
@override
void initState() {
super.initState();
ref.read(userClassProvider.notifier).fetchUser();
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
var result = ref.watch(userClassProvider);
if (result != null) {
_genderSelectedIndex = genders.indexOf(result.gender);
_selectedHobbies += List<String>.from(result.hobbies);
_interestSelectedIndex = genders.indexOf(result.interestIn);
_lookingForIndex = lookingForListEnums.indexOf(result.lookingFor);
dobController.text = DateFormat('dd-MM-yyyy')
.format(DateTime.parse(result.dob))
.toString();
heightController.text = result.height.toString();
}
}
.......the build context and widget....
UserModel.dart
import 'package:glint/main.dart';
import 'package:riverpod/riverpod.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
class UserClass {
final String id;
final String gender;
final List<String> hobbies;
final String interestIn;
final String lookingFor;
final String dob;
final int height;
final int minAge;
final int maxAge;
UserClass({
required this.id,
required this.minAge,
required this.maxAge,
required this.gender,
required this.hobbies,
required this.interestIn,
required this.lookingFor,
required this.dob,
required this.height,
});
factory UserClass.fromMap(Map<String, dynamic> data) {
return UserClass(
id: data['id'],
gender: data['gender'] as String,
hobbies: List<String>.from(data['hobbies']),
interestIn: data['interest_in'] as String,
lookingFor: data['looking_for'] as String,
dob: data['dob'] as String,
height: data['height'],
minAge: data['min_age'],
maxAge: data['max_age'],
);
}
factory UserClass.defaultUser(Map<String, dynamic> data) {
return UserClass(
id: '0',
gender: 'Male',
hobbies: [],
interestIn: '',
lookingFor: '',
dob: '',
height: 0,
minAge: 0,
maxAge: 0,
);
}
}
class UserNotifier extends StateNotifier<UserClass?> {
UserNotifier() : super(null);
Future<void> fetchUser() async {
UserResponse user = await supabase.auth.getUser();
if (user.user?.id != null) {
List<Map<String, dynamic>> response =
await supabase.from('users').select().eq('id', user.user!.id);
state = response.map((data) => UserClass.fromMap(data)).toList()[0];
}
}
}
var userClassProvider = StateNotifierProvider<UserNotifier, UserClass?>((ref) {
return UserNotifier();
});