setup provider

pull/2342/head
Miguel Beltran 4 months ago
parent 431177e4bb
commit df928c657d

@ -1,23 +1,18 @@
import 'package:compass_app/features/results/presentation/results_viewmodel.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class ResultsScreen extends StatelessWidget {
ResultsScreen({
const ResultsScreen({
super.key,
required this.resultsViewModel,
}) {
resultsViewModel.search();
}
final ResultsViewModel resultsViewModel;
});
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListenableBuilder(
listenable: resultsViewModel,
builder: (BuildContext context, Widget? child) {
if (resultsViewModel.loading) {
body: Consumer<ResultsViewModel>(
builder: (context, viewModel, child) {
if (viewModel.loading) {
return const CircularProgressIndicator();
}
return const Placeholder();

@ -7,7 +7,10 @@ import 'package:flutter/cupertino.dart';
class ResultsViewModel extends ChangeNotifier {
ResultsViewModel({
required SearchDestinationUsecase searchDestinationUsecase,
}) : _searchDestinationUsecase = searchDestinationUsecase;
}) : _searchDestinationUsecase = searchDestinationUsecase {
// Preload a search result
search();
}
final SearchDestinationUsecase _searchDestinationUsecase;

@ -1,12 +1,32 @@
import 'package:compass_app/features/results/business/usecases/search_destination_usecase.dart';
import 'package:compass_app/features/results/data/destination_repository.dart';
import 'package:compass_app/features/results/presentation/results_screen.dart';
import 'package:compass_app/features/results/presentation/results_viewmodel.dart';
import 'package:compass_app/routing/router.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() {
runApp(const MainApp());
// Configure dependencies
// These dependencies don't need to be in the widget tree (yet?)
final repository = DestinationRepository();
final searchDestinationUsecase =
SearchDestinationUsecase(repository: repository);
runApp(
MultiProvider(
providers: [
// ViewModels are injected into Views using Provider
ChangeNotifierProvider(
create: (_) => ResultsViewModel(
searchDestinationUsecase: searchDestinationUsecase,
),
// create this ViewModel only when needed
lazy: true,
),
],
child: const MainApp(),
),
);
}
class MainApp extends StatelessWidget {

@ -1,22 +1,14 @@
import 'package:compass_app/features/results/business/usecases/search_destination_usecase.dart';
import 'package:compass_app/features/results/data/destination_repository.dart';
import 'package:compass_app/features/results/presentation/results_screen.dart';
import 'package:compass_app/features/results/presentation/results_viewmodel.dart';
import 'package:go_router/go_router.dart';
final router = GoRouter(
initialLocation: '/results',
routes: [
GoRoute(
path: '/results',
builder: (context, state) {
return ResultsScreen(
resultsViewModel: ResultsViewModel(
searchDestinationUsecase: SearchDestinationUsecase(
repository: DestinationRepository(),
),
),
);
})
path: '/results',
builder: (context, state) {
return const ResultsScreen();
},
),
],
);

@ -10,6 +10,7 @@ dependencies:
flutter:
sdk: flutter
go_router: ^14.2.0
provider: ^6.1.2
dev_dependencies:
flutter_test:

Loading…
Cancel
Save