このようなシナリオを想像してください。
例: さまざまなジャンルの映画がたくさんあります。アイデアは TabBar であり、各タブにはジャンルのリスト ムービーが含まれています。この場合、 a ChangeNotifier
egMoviesBloc
はニーズにほぼ適合し、 とChangeNotifier
は何の関係もありませんGenre
。すべての TabBarView の子では、各タブが独自の状態ChangeNotifierProvider.value
を保持する必要があるため、ここでの使用は間違っています。そのため、ジャンルごとに を提供し、それを聞くために を提供します。というラッパークラスに入れました。MoviesBloc
ChangeNotifierProvider
MoviesBloc
Consumer
MoviesBlocView
結果: - 各タブを順番にスワイプすると、エラーは発生しません。- 多数のタブをスワイプする場合。ChangeNotifier
例: 突然最初のタブから最後のタブ (例: 20 個のタブがある) まで、コンソールは、各タブが個別に個別に作成されているにもかかわらず、破棄された再利用について不平を言いますChangeNotifier
。
再現コード
movies_bloc.dart
import 'package:flutter/material.dart';
class MoviesBloc extends ChangeNotifier {
List<Movie> _result;
BlocState _state = BlocState.idle;
Future<void> getMoviesWithGenre(Genre genre) async {
_setState(BlocState.loading);
await Future.delayed(_delayTime);
_result = List.generate(
20, (index) => Movie(id: index + 1, name: (index + 1).toString()));
_setState(BlocState.loaded);
}
List<Movie> get result => _result;
bool get loading => _state == BlocState.loading;
BlocState get state => _state;
void _setState(BlocState state) {
_state = state;
notifyListeners();
}
}
enum BlocState {
idle,
loading,
loaded,
}
class Movie {
Movie({
this.id,
this.name,
});
num id;
String name;
}
class Genre {
Genre({
this.id,
this.name,
});
num id;
String name;
}
const _delayTime = Duration(seconds: 2);
movies_bloc_view.dart
class MoviesBlocView extends StatelessWidget {
const MoviesBlocView({
Key key,
@required this.bloc,
@required this.loadedBuilder,
}) : assert(bloc != null),
assert(loadedBuilder != null),
super(key: key);
final MoviesBloc bloc;
final Widget Function(BuildContext context, List<Movie> result) loadedBuilder;
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<MoviesBloc>(
create: (context) => bloc,
child: Consumer<MoviesBloc>(
builder: (context, bloc, child) {
switch (bloc.state) {
case BlocState.idle:
return Container();
case BlocState.loading:
return const Center(child: CircularProgressIndicator());
case BlocState.loaded:
return loadedBuilder(context, bloc.result);
default:
return Container();
}
},
),
);
}
}
main.dart
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'movies_bloc.dart';
import 'movies_bloc_view.dart';
List<Genre> genres = List.generate(
20, (index) => Genre(id: index + 1, name: (index + 1).toString()));
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: DefaultTabController(
length: genres.length,
child: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
SliverAppBar(
forceElevated: innerBoxIsScrolled,
title: const Text('Provider'),
bottom: TabBar(
isScrollable: true,
tabs: genres
.map((genre) => Tab(child: Text(genre.name)))
.toList(growable: false),
),
),
];
},
body: TabBarView(
children: genres.map(
(genre) {
// use wrapper
return MoviesBlocView(
bloc: MoviesBloc()..getMoviesWithGenre(genre),
loadedBuilder: (context, movies) => MoviesView(
movies: movies,
genre: genre,
),
);
// or use provider directly
return ChangeNotifierProvider<MoviesBloc>(
create: (context) => MoviesBloc()..getMoviesWithGenre(genre),
child: Consumer<MoviesBloc>(
builder: (context, bloc, child) {
switch (bloc.state) {
case BlocState.idle:
return Container();
case BlocState.loading:
return const Center(
child: CircularProgressIndicator());
case BlocState.loaded:
return MoviesView(
movies: bloc.result,
genre: genre,
);
default:
return Container();
}
},
),
);
},
).toList(growable: false),
),
),
),
);
}
}
class MoviesView extends StatefulWidget {
const MoviesView({
Key key,
@required this.movies,
@required this.genre,
}) : assert(movies != null),
assert(genre != null),
super(key: key);
final List<Movie> movies;
final Genre genre;
@override
_MoviesViewState createState() => _MoviesViewState();
}
class _MoviesViewState extends State<MoviesView>
with AutomaticKeepAliveClientMixin {
@override
bool get wantKeepAlive => true;
@override
Widget build(BuildContext context) {
super.build(context); // AutomaticKeepAliveClientMixin
return ListView.builder(
shrinkWrap: true,
itemCount: widget.movies.length,
itemBuilder: (_, index) {
return ListTile(
title: Text('Movie: ${widget.movies[index].name}'),
trailing: Text('Genre: ${widget.genre.name}'),
);
},
);
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
);
}
}
更新:
- wrapper を使用すると、作成時
MoviesBlocView
に呼び出すかどうかに関係なく、コンソールは引き続き文句を言いますgetMoviesWithGenre
MoviesBloc
════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building Consumer<MoviesBloc>(dirty, dependencies: [_DefaultInheritedProviderScope<MoviesBloc>]):
A MoviesBloc was used after being disposed.
Once you have called dispose() on a MoviesBloc, it can no longer be used.
The relevant error-causing widget was
Consumer<MoviesBloc>
lib\test_tabs\movies_bloc_view.dart:23
When the exception was thrown, this was the stack
#0 ChangeNotifier._debugAssertNotDisposed.<anonymous closure>
package:flutter/…/foundation/change_notifier.dart:105
#1 ChangeNotifier._debugAssertNotDisposed
package:flutter/…/foundation/change_notifier.dart:111
#2 ChangeNotifier.addListener
package:flutter/…/foundation/change_notifier.dart:141
#3 ListenableProvider._startListening
package:provider/src/listenable_provider.dart:87
#4 _CreateInheritedProviderState.value
package:provider/src/inherited_provider.dart:433
...
- を使用しない場合
MoviesBlocView
、getMoviesWithGenre
コンソールは、MoviesBloc
作成時に呼び出そうとすると文句を言います。今では別のエラーログがあるのは奇妙です
E/flutter (30300): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: A MoviesBloc was used after being disposed.
E/flutter (30300): Once you have called dispose() on a MoviesBloc, it can no longer be used.
[38;5;244mE/flutter (30300): #0 ChangeNotifier._debugAssertNotDisposed.<anonymous closure>[39;49m
[38;5;244mE/flutter (30300): #1 ChangeNotifier._debugAssertNotDisposed[39;49m
[38;5;244mE/flutter (30300): #2 ChangeNotifier.notifyListeners[39;49m
[38;5;248mE/flutter (30300): #3 MoviesBloc._setState[39;49m
[38;5;248mE/flutter (30300): #4 MoviesBloc.getMoviesWithGenre[39;49m
E/flutter (30300): <asynchronous suspension>
[38;5;248mE/flutter (30300): #5 HomePage.build.<anonymous closure>.<anonymous closure>[39;49m
[38;5;248mE/flutter (30300): #6 _CreateInheritedProviderState.value[39;49m
[38;5;248mE/flutter (30300): #7 _InheritedProviderScopeMixin.value[39;49m
[38;5;248mE/flutter (30300): #8 Provider.of[39;49m
[38;5;248mE/flutter (30300): #9 Consumer.buildWithChild[39;49m
[38;5;248mE/flutter (30300): #10 SingleChildStatelessWidget.build[39;49m
[38;5;244mE/flutter (30300): #11 StatelessElement.build[39;49m
[38;5;248mE/flutter (30300): #12 SingleChildStatelessElement.build[39;49m
[38;5;244mE/flutter (30300): #13 ComponentElement.performRebuild[39;49m
[38;5;244mE/flutter (30300): #14 Element.rebuild[39;49m
[38;5;244mE/flutter (30300): #15 ComponentElement._firstBuild[39;49m
[38;5;244mE/flutter (30300): #16 ComponentElement.mount[39;49m
[38;5;248mE/flutter (30300): #17 SingleChildWidgetElementMixin.mount[39;49m
[38;5;244mE/flutter (30300): #18 Element.inflateWidget[39;49m
[38;5;244mE/flutter (30300): #19 Element.updateChild[39;49m
[38;5;244mE/flutter (30300): #20 ComponentElement.performRebuild[39;49m
[38;5;248mE/flutter (30300): #21 _InheritedProviderScopeMixin.performRebuild[39;49m
[38;5;244mE/flutter (30300): #22 Element.rebuild[39;49m
[38;5;244mE/flutter (30300): #23 ComponentElement._firstBuild[39;49m
[38;5;244mE/flutter (30300): #24 ComponentElement.mount[39;49m
[38;5;244mE/flutter (30300): #25 Element.inflateWidget[39;49m
[38;5;244mE/flutter (30300): #26 Element.updateChild[39;49m
[38;5;244mE/flutter (30300): #27 ComponentElement.performRebuild[39;49m
[38;5;244mE/flutter (30300): #28 Element.rebuild[39;49m
[38;5;244mE/flutter (30300): #29 ComponentElement._firstBuild[39;49m
[38;5;244mE/flutter (30300): #30 ComponentElement.mount[39;49m
[38;5;248mE/flutter (30300): #31 SingleChildWidgetElementMixin.mount[39;49m
[38;5;244mE/flutter (30300): #32 Element.inflateWidget[39;49m
[38;5;244mE/flutter (30300): #33 Element.updateChild[39;49m
[38;5;244mE/flutter (30300): #34 ComponentElement.performRebuild[39;49m
[38;5;244mE/flutter (30300): #35 Element.rebuild[39;49m
[38;5;244mE/flutter (30300): #36 ComponentElement._firstBuild[39;49m
[38;5;244mE/flutter (30300): #37 ComponentElement.mount[39;49m
[38;5;244mE/flutter (30300): #38 Element.inflateWidget[39;49m
[38;5;244mE/flutter (30300): #39 Element.updateChild[39;49m
[38;5;244mE/flutter (30300): #40 SingleChildRenderObjectElement.mount[39;49m
[38;5;244mE/flutter (30300): #41 Element.inflateWidget[39;49m
[38;5;244mE/flutter (30300): #42 Element.updateChild[39;49m
E/flutter (30300): #43 SingleChildRenderObjectElement.mount (package:flutter/sr
E/flutter (30300): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: A MoviesBloc was used after being disposed.
E/flutter (30300): Once you have called dispose() on a MoviesBloc, it can no longer be used.
[38;5;244mE/flutter (30300): #0 ChangeNotifier._debugAssertNotDisposed.<anonymous closure>[39;49m
[38;5;244mE/flutter (30300): #1 ChangeNotifier._debugAssertNotDisposed[39;49m
[38;5;244mE/flutter (30300): #2 ChangeNotifier.notifyListeners[39;49m
[38;5;248mE/flutter (30300): #3 MoviesBloc._setState[39;49m
[38;5;248mE/flutter (30300): #4 MoviesBloc.getMoviesWithGenre[39;49m
E/flutter (30300): <asynchronous suspension>
[38;5;248mE/flutter (30300): #5 HomePage.build.<anonymous closure>.<anonymous closure>[39;49m
[38;5;248mE/flutter (30300): #6 _CreateInheritedProviderState.value[39;49m
[38;5;248mE/flutter (30300): #7 _InheritedProviderScopeMixin.value[39;49m
[38;5;248mE/flutter (30300): #8 Provider.of[39;49m
[38;5;248mE/flutter (30300): #9 Consumer.buildWithChild[39;49m
[38;5;248mE/flutter (30300): #10 SingleChildStatelessWidget.build[39;49m
[38;5;244mE/flutter (30300): #11 StatelessElement.build[39;49m
[38;5;248mE/flutter (30300): #12 SingleChildStatelessElement.build[39;49m
[38;5;244mE/flutter (30300): #13 ComponentElement.performRebuild[39;49m
[38;5;244mE/flutter (30300): #14 Element.rebuild[39;49m
[38;5;244mE/flutter (30300): #15 ComponentElement._firstBuild[39;49m
[38;5;244mE/flutter (30300): #16 ComponentElement.mount[39;49m
[38;5;248mE/flutter (30300): #17 SingleChildWidgetElementMixin.mount[39;49m
[38;5;244mE/flutter (30300): #18 Element.inflateWidget[39;49m
[38;5;244mE/flutter (30300): #19 Element.updateChild[39;49m
[38;5;244mE/flutter (30300): #20 ComponentElement.performRebuild[39;49m
[38;5;248mE/flutter (30300): #21 _InheritedProviderScopeMixin.performRebuild[39;49m
[38;5;244mE/flutter (30300): #22 Element.rebuild[39;49m
[38;5;244mE/flutter (30300): #23 ComponentElement._firstBuild[39;49m
[38;5;244mE/flutter (30300): #24 ComponentElement.mount[39;49m
[38;5;244mE/flutter (30300): #25 Element.inflateWidget[39;49m
[38;5;244mE/flutter (30300): #26 Element.updateChild[39;49m
[38;5;244mE/flutter (30300): #27 ComponentElement.performRebuild[39;49m
[38;5;244mE/flutter (30300): #28 Element.rebuild[39;49m
[38;5;244mE/flutter (30300): #29 ComponentElement._firstBuild[39;49m
[38;5;244mE/flutter (30300): #30 ComponentElement.mount[39;49m
[38;5;248mE/flutter (30300): #31 SingleChildWidgetElementMixin.mount[39;49m
[38;5;244mE/flutter (30300): #32 Element.inflateWidget[39;49m
[38;5;244mE/flutter (30300): #33 Element.updateChild[39;49m
[38;5;244mE/flutter (30300): #34 ComponentElement.performRebuild[39;49m
[38;5;244mE/flutter (30300): #35 Element.rebuild[39;49m
[38;5;244mE/flutter (30300): #36 ComponentElement._firstBuild[39;49m
[38;5;244mE/flutter (30300): #37 ComponentElement.mount[39;49m
[38;5;244mE/flutter (30300): #38 Element.inflateWidget[39;49m
[38;5;244mE/flutter (30300): #39 Element.updateChild[39;49m
[38;5;244mE/flutter (30300): #40 SingleChildRenderObjectElement.mount[39;49m
[38;5;244mE/flutter (30300): #41 Element.inflateWidget[39;49m
[38;5;244mE/flutter (30300): #42 Element.updateChild[39;49m
E/flutter (30300): #43 SingleChildRenderObjectElement.mount (package:flutter/sr
- 作成時に呼び出さなくてもエラーは発生しませんが、そのようなカスケード
getMoviesWithGenre
を行わないと、作成時にリクエストをフェッチするなどの方法がわからないため、意味がありません。MoviesBloc
どんな助けでも大歓迎です!
アップデート
いくつかのデバッグの後、TabBar の奇妙な動作が原因であることがわかりました。最も近い選択されたタブも事前にロードしてから、同じタブで1回ではなく2回続けて破棄しようとします。
レミが指摘したようにMoviesBloc
、この検証を正常に機能するように追加した後、破棄されているときにnotifyListeners()を呼び出さないことを確認する必要がありMoviesBloc
ます
bool _mounted = true;
bool get mounted => _mounted;
@override
void dispose() {
_mounted = false;
super.dispose();
}
void _setState(BlocState state) {
if (!mounted) return;
_state = state;
notifyListeners();
}
しかし、これはMoviesBloc
作成時に関数を呼び出すため、関数が破棄された後に関数が呼び出されるとは決して想像できないため、期待した動作ではありません。
I/flutter ( 1431): MoviesBloc - getMoviesWithGenre: 16
I/flutter ( 1431): MoviesBloc - getMoviesWithGenre: 16
I/flutter ( 1431): MoviesBloc - getMoviesWithGenre: 17 --> selected tab
I/flutter ( 1431): MoviesBloc - getMoviesWithGenre: 19
I/flutter ( 1431): MoviesBloc - getMoviesWithGenre: 20 --> selected tab
I/flutter ( 1431): MoviesBloc - getMoviesWithGenre: 19
I/flutter ( 1431): MoviesBloc - getMoviesWithGenre: 8
I/flutter ( 1431): MoviesBloc - getMoviesWithGenre: 7 --> selected tab
I/flutter ( 1431): MoviesBloc - getMoviesWithGenre: 8
この時点で、TabBar と TabBarView がどうなっているのか本当にわかりません。なぜTabBarがそれを行うのかを誰かが説明できれば、私は非常に感謝しています!