我正在使用Flutter。我有一个带有3个标签的简单应用。每个选项卡中都有一个带有ListView的RefreshIndicator。这些行是用另一种方法构建的。这是代码:
@override
Widget build(BuildContext context) {
final GlobalKey<RefreshIndicatorState> _RIKey1 = new GlobalKey<RefreshIndicatorState>();
final GlobalKey<RefreshIndicatorState> _RIKey2 = new GlobalKey<RefreshIndicatorState>();
final GlobalKey<RefreshIndicatorState> _RIKey3 = new GlobalKey<RefreshIndicatorState>();
debugPrint(_RIKey1.toString());
debugPrint(_RIKey2.toString());
debugPrint(_RIKey3.toString());
return new Scaffold(
body: new DefaultTabController(
length: 3,
child: new Scaffold(
appBar: new AppBar(
bottom: new TabBar(
tabs: [
new Tab(icon: new Icon(Icons.view_list)),
new Tab(icon: new Icon(Icons.hotel)),
new Tab(icon: new Icon(Icons.assessment)),
],
),
title: new Text('Data'),
),
body: new TabBarView(
children: [
new RefreshIndicator(
key: _RIKey1,
onRefresh: _actualizoData,
child: new ListView.builder(
padding: new EdgeInsets.only(top: 5.0),
itemCount: linea_reservas.length * 2,
itemBuilder: (BuildContext context, int position) {
if (position.isOdd) return new Divider();
final index = position ~/ 2;
return _buildRow(index);
}),
),
new RefreshIndicator(
key: _RIKey2,
onRefresh: _actualizoData,
child: new ListView.builder(
padding: new EdgeInsets.only(top: 8.0),
itemCount: linea_inouthouse.length * 2,
itemBuilder: (BuildContext context, int position) {
if (position.isOdd) return new Divider();
final index = position ~/ 2;
return _buildRowInOutHouse(index);
}),
),
new RefreshIndicator(
key: _RIKey3,
onRefresh: _actualizoData,
child: new ListView.builder(
padding: new EdgeInsets.only(top: 5.0),
itemCount: linea_ocupacion.length * 2,
itemBuilder: (BuildContext context, int position) {
if (position.isOdd) return new Divider();
final index = position ~/ 2;
return _buildRowOcupacion(index);
}),
),
],
),
),
),
);
}
Run Code Online (Sandbox Code Playgroud)
我添加了debugPrints,输出为6行,而不是3行。
I/flutter ( 5252): [LabeledGlobalKey<RefreshIndicatorState>#4d76c]
I/flutter ( 5252): [LabeledGlobalKey<RefreshIndicatorState>#59b9e]
I/flutter ( 5252): [LabeledGlobalKey<RefreshIndicatorState>#2c88b]
I/flutter ( 5252): [LabeledGlobalKey<RefreshIndicatorState>#7bd42]
I/flutter ( 5252): [LabeledGlobalKey<RefreshIndicatorState>#1c984]
I/flutter ( 5252): [LabeledGlobalKey<RefreshIndicatorState>#dbe20]
Run Code Online (Sandbox Code Playgroud)
该应用程序可以运行,但是几次更改标签后,它因以下错误而崩溃:
I/flutter ( 5252): ??? EXCEPTION CAUGHT BY WIDGETS LIBRARY ????????????????????????????????????????????????????????????
I/flutter ( 5252): The following assertion was thrown building NotificationListener<KeepAliveNotification>:
I/flutter ( 5252): Multiple widgets used the same GlobalKey.
I/flutter ( 5252): The key [LabeledGlobalKey<RefreshIndicatorState>#7bd42] was used by multiple widgets. The parents of
I/flutter ( 5252): those widgets were:
I/flutter ( 5252): - RepaintBoundary-[<[LabeledGlobalKey<RefreshIndicatorState>#7bd42]>](renderObject:
I/flutter ( 5252): RenderRepaintBoundary#60a4a DETACHED)
I/flutter ( 5252): - RepaintBoundary-[<[LabeledGlobalKey<RefreshIndicatorState>#7bd42]>](renderObject:
I/flutter ( 5252): RenderRepaintBoundary#c8cdb NEEDS-LAYOUT NEEDS-PAINT)
I/flutter ( 5252): A GlobalKey can only be specified on one widget at a time in the widget tree.
Run Code Online (Sandbox Code Playgroud)
密钥是在Build方法中生成的,所以,我不明白为什么会Multiple widgets used the same GlobalKey出错
为什么会再次生成密钥,为什么它不是唯一的?我并不是在说一千个意图,在选项卡之间切换4到5次后,错误就会出现。感谢您的任何帮助。
bab*_*thy 16
您可以“手动”创建密钥并使用静态/常量值吗?例如...
import 'package:flutter/widgets.dart';
class RIKeys {
static final riKey1 = const Key('__RIKEY1__');
static final riKey2 = const Key('__RIKEY2__');
static final riKey3 = const Key('__RIKEY3__');
}
Run Code Online (Sandbox Code Playgroud)
然后在...
body: new TabBarView(
children: [
new RefreshIndicator(new RefreshIndicator(
// Use the Manual Static Value instead ...
key: RIKeys.riKey1,
onRefresh: _actualizoData,
child: new ListView.builder(
padding: new EdgeInsets.only(top: 5.0),
itemCount: linea_reservas.length * 2,
itemBuilder: (BuildContext context, int position) {
if (position.isOdd) return new Divider();
final index = position ~/ 2;
return _buildRow(index);
}),
),
Run Code Online (Sandbox Code Playgroud)
我在类似的情况下做到了这一点,并取得了一些成功……尤其是使用 redux 或其他类似的模式……
moh*_*esR 16
我想创建一个表单键列表。
只需更改GlobalKey为GlobalObjectKey
像下面这样。
final List<GlobalObjectKey<FormState>> formKeyList = List.generate(10, (index) => GlobalObjectKey<FormState>(index));
Run Code Online (Sandbox Code Playgroud)
对于那些需要继续使用 GlobalKey 的人:
import 'package:flutter/widgets.dart';
class JosKeys {
static final josKeys1 = GlobalKey();
static final josKeys2 = GlobalKey();
}
Run Code Online (Sandbox Code Playgroud)
使用如下:
[
CoachTutorialModel(
ids: "post",
globalKeys: JosKeys.josKeys1,
icons: Icon(
Icons.message,
color: Colors.white,
size: 90,
),
...
),
CoachTutorialModel(
ids: "post2",
globalKeys: JosKeys.josKeys2,
icons: Icon(
Icons.message,
color: Colors.white,
size: 90,
),
...
),
]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4365 次 |
| 最近记录: |