У меня есть экран, который получает состояния через BlocListener
, а ListView.builder
создает пользовательский интерфейс на основе List<UserRoute> routes
. Поэтому я использую setState()
, чтобы назначить новые значения для routes
, и пользовательский интерфейс должен обновиться, но это не так. Я печатаю значения для состояния и маршрутов в BlocListener, и все они верны. Затем я провел тест, используя только ListView
с одним дочерним элементом Text(routes.isNotEmpty ? '${routes.first.routeName}' : 'routes is Empty')
, и даже после получения состояния дочерний элемент выводит «маршруты пустые», то есть routes
не обновляется. Вы видите, что происходит?
Это экран:
class SelectRouteScreen extends StatefulWidget {
bool isIOS = Platform.isIOS ? true : false;
User user;
String routeName;
SelectRouteScreen({@required this.user});
@override
_SelectRouteScreenState createState() => _SelectRouteScreenState();
}
class _SelectRouteScreenState extends State<SelectRouteScreen> {
@override
Widget build(BuildContext context) {
AudioCache cache = new AudioCache();
List<UserRoute> routes = [];
cache.loadAll(['click.mp3', 'tableViewOpen.mp3', 'tableViewClose.mp3']);
return BlocListener<RouteBloc, RouteState>(
listener: (BuildContext context, RouteState state) {
if (state is RoutesLoaded) {
print(' SelectRouteScreen BlocListener state received');
setState(() {
routes = [];
routes = state.userRoutes;
});
print(
'SelectRouteScreen BlocListener incoming state routes are ${state.userRoutes.length}');
print('SelectRouteScreen BlocListener routes are: $routes');
print(
'SelectRouteScreen BlocListener first route name is: ${routes.first.routeName}');
}
},
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.transparent,
elevation: 0,
centerTitle: true,
leading: IconButton(
icon: widget.isIOS
? Icon(CupertinoIcons.back)
: Icon(Icons.arrow_back),
color: Colors.redAccent,
onPressed: () {
cache.play('tableViewClose.mp3');
Navigator.pop(context, widget.routeName);
}),
title: Text(
// '${routes.first.routeName}',
'Seleziona tragitto',
style: TextStyle(
color: Colors.orange,
fontSize: 22,
fontWeight: FontWeight.w500,
letterSpacing: 1),
),
),
body: Container(
padding: EdgeInsets.all(20),
color: Colors.amber,
child: ListView(
children: <Widget>[
Text(routes.isNotEmpty
? '${routes.first.routeName}'
: 'routes is Empty ${routes.length}') // prints 'route is empty 0'
],
),
// child: ListView.builder(
// itemCount: routes.length,
// itemBuilder: (BuildContext context, int index) => RouteCell(
// routeName: '${routes[index].routeName}',
// isSelected: false,
// onTap: () {
// widget.routeName = routes[index].routeName;
// },
// onTapCancel: () {
// widget.routeName = null;
// },
// ),
// ),
),
),
);
}
}
и это отпечатки:
I/flutter ( 350): SelectRouteScreen BlocListener state received
I/flutter ( 350): SelectRouteScreen BlocListener incoming state routes are 1
I/flutter ( 350): SelectRouteScreen BlocListener routes are: [Route: {RO-112c206e-b1c2-4b03-969b-b07f544cf160,json try,0.22044000000000002,57.0,[LatLng(latitude:44.500714, longitude:11.335623), LatLng(latitude:44.500714, longitude:11.335623), LatLng(latitude:44.500714, longitude:11.335623), LatLng(latitude:44.500714, longitude:11.335623), LatLng(latitude:44.50054, longitude:11.335546), LatLng(latitude:44.50054, longitude:11.335546), LatLng(latitude:44.500518, longitude:11.335627), LatLng(latitude:44.500518, longitude:11.335627), LatLng(latitude:44.500518, longitude:11.335627), LatLng(latitude:44.500518, longitude:11.335627), LatLng(latitude:44.500492, longitude:11.335626), LatLng(latitude:44.500492, longitude:11.335626), LatLng(latitude:44.500471, longitude:11.335714), LatLng(latitude:44.500471, longitude:11.335714), LatLng(latitude:44.500471, longitude:11.335714), LatLng(latitude:44.500469, longitude:11.335778), LatLng(latitude:44.500469, longitude:11.335778), LatLng(latitude:44.500472, longitude:11.335865), LatLng(latitude:
I/flutter ( 350): SelectRouteScreen BlocListener first route name is: json try
Как может быть, что отпечатки показывают значения но при их использовании я не нахожу ничего?