我正在努力实现这种前景。 [1]:https://i.stack.imgur.com/6COFI.png这是我第一次使用provider。相同的代码与todo应用程序配合良好。但是我无法使用此应用程序的代码。我试图调整代码,但徒劳无功。
错误消息是══╡手势引起的异常CA handling在处理手势时引发了以下断言:尝试侦听公开的值与提供程序一起,从窗口小部件树的外部。
这很可能是由于调用Provider.of的事件处理程序(如按钮的onPressed)未传递listen: false
引起的。
要修复,请写:Provider.of(上下文,听:false);
它不受支持,因为当窗口小部件树不在乎该值时,它可能无意义地重建与事件处理程序关联的窗口小部件。
使用的上下文是:AddPlayersScreen(状态: _AddPlayersScreenState#da0e7)'package:provider / src / provider.dart':失败的断言:第242行pos 7:'context.owner.debugBuilding || 听==假|| debugIsInInheritedProviderUpdate'
引发异常时,这是堆栈: #2 Provider.of(package:provider / src / provider.dart:242:7) #3 _AddPlayersScreenState.build。 (package:badminton_app / screens / add_players_screen.dart:73:34) #4 _InkResponseState._handleTap(包:flutter / src / material / ink_well.dart:992:19) #5 _InkResponseState.build。 (包装:flutter / src / material / ink_well.dart:1098:38) #6 GestureRecognizer.invokeCallback(package:flutter / src / gestures / recognizer.dart:184:24) #7 TapGestureRecognizer.handleTapUp(软件包:flutter / src / gestures / tap.dart:524:11) #8 BaseTapGestureRecognizer._checkUp(package:flutter / src / gestures / tap.dart:284:5) #9 BaseTapGestureRecognizer.acceptGesture(包:flutter / src / gestures / tap.dart:256:7) #10 GestureArenaManager.sweep(package:flutter / src / gestures / arena.dart:158:27) #11 GestureBinding.handleEvent(package:flutter / src / gestures / binding.dart:224:20) #12 GestureBinding.dispatchEvent(package:flutter / src / gestures / binding.dart:200:22) #13 GestureBinding._handlePointerEvent(package:flutter / src / gestures / binding.dart:158:7) #14 GestureBinding._flushPointerEventQueue(package:flutter / src / gestures / binding.dart:104:7) #15 GestureBinding._handlePointerDataPacket(包:flutter / src / gestures / binding.dart:88:7) #19 _invoke1(dart:ui / hooks.dart:267:10) #20 _dispatchPointerDataPacket(dart:ui / hooks.dart:176:5)(从_AssertionError类和dart:async类中删除了5帧)
处理程序:“ onTap”识别器:TapGestureRecognizer#65790═════════════════════════════════════ ══════════════════════════════════════════════════ ═════════════
import 'package:badminton_app/constants.dart';
import 'package:badminton_app/model/player_list.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:badminton_app/constants.dart';
import 'package:badminton_app/model/players_data.dart';
import 'package:badminton_app/widgets/check_cards.dart';
TextEditingController _controller = TextEditingController();
class AddPlayersScreen extends StatefulWidget {
@override
_AddPlayersScreenState createState() => _AddPlayersScreenState();
}
class _AddPlayersScreenState extends State<AddPlayersScreen> {
String newText;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xff07021A),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(25.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: 30.0,
),
Text(
'Add Players',
style: TextStyle(
color: Colors.white,
fontSize: 40.0,
fontFamily: 'Montserrat'),
),
SizedBox(
width: 400.0,
height: 50.0,
child: Divider(
height: 10.0,
color: Color(0xff525274),
),
),
Container(
constraints: BoxConstraints.tight(Size(400.0, 70.0)),
child: TextField(
cursorColor: Color(0xffA8A3BE),
style: TextStyle(color: Color(0xffA8A3BE), fontSize: 20.0),
controller: _controller,
onChanged: (newValue) {
newText = newValue;
},
enabled: true,
decoration: InputDecoration(
suffix: IconButton(
onPressed: () {
_controller.clear();
},
icon: CircleAvatar(
radius: 13.0,
backgroundColor: Color(0xff939393),
child: Icon(
Icons.clear,
size: 25.0,
color: Color(0xff585179),
),
),
),
suffixIcon: IconButton(
onPressed: () {
Provider.of<PlayerData>(context).changeString(newText);
},
icon: Container(
height: 100.0,
width: 50.0,
decoration: BoxDecoration(
color: Colors.yellow,
borderRadius: BorderRadius.only(
topRight: Radius.circular(5.0),
bottomRight: Radius.circular(5.0))),
child: Icon(
Icons.add,
color: Colors.black,
)),
),
hintText: "New member......",
hintStyle: TextStyle(
fontSize: 20.0,
fontFamily: "Montserrat",
color: Color(
0xffA199C6,
),
),
filled: true,
fillColor: Color(0xff585179),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
borderSide: BorderSide.none),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(10.0),
),
borderSide: BorderSide.none),
),
),
),
SizedBox(
height: 20.0,
),
Expanded(
child: PlayerList(),
),
],
),
),
),
);
}
}
import 'dart:collection';
import 'package:flutter/cupertino.dart';
import 'package:badminton_app/model/players.dart';
class PlayerData extends ChangeNotifier {
List<Players> _playerData = [];
UnmodifiableListView<Players> get playerData {
return UnmodifiableListView(_playerData);
}
toggleSelect(Players players) {
players.toggle();
notifyListeners();
}
int get getLength {
return playerData.length;
}
changeString(newString) {
_playerData.add(Players(name: newString));
notifyListeners();
}
removeString(Players player) {
_playerData.remove(player);
notifyListeners();
}
}
import 'package:badminton_app/model/players_data.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:badminton_app/model/players.dart';
class CheckCards extends StatelessWidget {
final String name;
final bool isSelected;
final Function callBack;
CheckCards({this.name, this.isSelected = false, this.callBack});
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.only(bottom: 20.0),
child: Stack(
children: [
Positioned(
left: 25.0,
top: 17.0,
child: Center(
child: InkWell(
onTap: callBack,
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(7.0)),
color: Colors.yellow),
child: Padding(
padding: const EdgeInsets.all(1.0),
child: isSelected
? Icon(
Icons.brightness_1,
size: 30.0,
color: Color(0xff585179),
)
: Icon(
Icons.stop,
size: 30.0,
color: Color(0xff585179),
),
),
),
),
),
),
Positioned(
left: 80.0,
top: 22.0,
child: Text(
'Sai Deepak',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20.0,
color: Colors.white,
fontFamily: 'Montserrat'),
),
),
],
),
height: 70.0,
width: 500.0,
decoration: BoxDecoration(
color: isSelected ? Color(0xff21213C) : Color(0xff585179),
borderRadius: BorderRadius.all(Radius.circular(10.0))),
);
}
}
import 'package:badminton_app/model/players.dart';
import 'package:badminton_app/model/players_data.dart';
import 'package:badminton_app/widgets/check_cards.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:badminton_app/widgets/tile.dart';
class PlayerList extends StatefulWidget {
@override
_PlayerListState createState() => _PlayerListState();
}
class _PlayerListState extends State<PlayerList> {
@override
Widget build(BuildContext context) {
return Consumer<PlayerData>(builder: (context, data, child) {
return ListView.builder(
itemBuilder: (context, index) {
final playo = data.playerData[index];
return CheckCards(
name: playo.name,
isSelected: playo.isDone,
callBack: () {
data.toggleSelect(playo);
},
);
},
itemCount: data.getLength,
);
});
}
}
有人可以帮我这个忙吗?这东西好几天都在搞砸。预先感谢。
答案 0 :(得分:0)
在onPressed()中使用
Provider.of(上下文,监听:false);
检查您是否通过了listen:false 如果您更改本地数据,也可以使用setState