Как добавить Инъекцию зависимостей во флаттер с провайдером - PullRequest
0 голосов
/ 07 апреля 2020
void main() => runApp(MultiProvider(providers: [
      ChangeNotifierProvider(
        create: (BuildContext context) => QuoteViewModel(),
      ),
      ChangeNotifierProvider(
        create: (BuildContext context) => AuthorViewModel(),
      ),
    ], child: MyApp()));

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Poems',
      home: PoemsListPage(),
    );
  }
}

main.dart

class QuoteListPage extends StatefulWidget {
  @override
  _QuoteListPageState createState() => _QuoteListPageState();
}

class _QuoteListPageState extends State<QuoteListPage> {
  @override
  Widget build(BuildContext context) {
    final model = Provider.of<QuoteViewModel>(context);
    return Scaffold(
      appBar: AppBar(title: Text('Poems List')),
      body: _buildBody(model.getQuotes()),
    );
  }

  Widget _buildBody(List<Quote> quotes) => ListView(
        children: quotes
            .map((quote) => ListTile(
                  title: Text(quote.body),
                ))
            .toList(),
      );
}

quoteListPage.dart

class Quote {
  final String id;
  final String authorId;
  final String body;

  Quote(this.id, this.authorId, this.body);
}

class Author {
  final String id;
  final String name;

  Author(this.id, this.name);
}

models.dart

class Repository {
  final Api _api = Api();
  final Database _database = Api();

  List<Quote> getQuotes() => [];

  List<Author> getAuthors() => [];
}

repository.dart

class QuoteViewModel with ChangeNotifier {
  final Repository _repository = Repository();

  List<Quote> getQuotes() => _repository.getQuotes();
}

class AuthorViewModel with ChangeNotifier {
  final Repository _repository = Repository();

  List<Author> getAuthors() => _repository.getAuthors();
}

notifiers.dart

Я пытался использовать ProxyProvider после прочтения статьи на Средняя , но она заканчивается ошибкой "Это, вероятно, ошибка, так как поставщик не будет автоматически обновлять иждивенцев "

List<SingleChildWidget> globalProviders = [
  Provider.value(value: Api()),
  Provider.value(value: Database()),
  ProxyProvider2<Api, Database, Repository>(
    update: (_, api, database, __) => Repository(api, database),
  ),
  ProxyProvider<Repository, QuoteViewModel>(
    update: (_, repo, __) => QuoteViewModel(repo),
  ),
  ProxyProvider<Repository, AuthorViewModel>(
    update: (_, repo, __) => AuthorViewModel(repo),
  )
];

void main() => runApp(
      MultiProvider(
        providers: globalProviders,
        child: MyApp(),
      ),
    );

main.dart (обновлено)

class Repository {
  final Api _api;
  final Database _database;

  Repository({@required api, @required database})
      : _api = api,
        _database = database;

  List<Quote> getQuotes() => [];

  List<Author> getAuthors() => [];
}

repository.dart (обновлено)

class QuoteViewModel with ChangeNotifier {
  final Repository _repository;

  QuoteViewModel({@required repository}) : _repository = repository;

  List<Quote> getQuotes() => _repository.getQuotes();
}

class AuthorViewModel with ChangeNotifier {
  final Repository _repository;

  AuthorViewModel({@required repository}) : _repository = repository;

  List<Author> getAuthors() => _repository.getAuthors();
}

notifiers.dart ( Обновлено)

...