Files
food_hub_app/lib/provider/user_provider.dart

52 lines
1.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:food_hub_app/apis/moment.dart';
import 'package:food_hub_app/apis/user.dart';
import 'package:food_hub_app/models/moment.dart';
import 'package:food_hub_app/models/session.dart';
class UserProvider with ChangeNotifier {
late User currentUser = User.getEmpty();
bool isLoading = false;
String error = '';
List<Moment> momentList = [];
Future<void> refreshUser(int id) async {
if (isLoading) return;
try {
isLoading = true;
error = '';
notifyListeners();
final result = await queryUserApi(id);
currentUser = result;
} catch (e) {
error = '加载数据失败: $e';
debugPrint('加载数据失败: $e');
} finally {
isLoading = false;
notifyListeners();
}
}
Future<void> queryMomentByUserId(int userId) async {
if (isLoading) return;
try {
isLoading = true;
error = '';
notifyListeners();
final result = await queryMomentListByUserIdApi(userId);
momentList = result;
notifyListeners();
} catch (e) {
error = '加载数据失败: $e';
debugPrint('加载数据失败: $e');
} finally {
isLoading = false;
notifyListeners();
}
}
}