在本文中,我们将带你了解mapEventToState仅触发一次在这篇文章中,我们将为您详细介绍mapEventToState仅触发一次的方方面面,并解答onreachbottom只触发一次常见的疑惑
在本文中,我们将带你了解mapEventToState仅触发一次在这篇文章中,我们将为您详细介绍mapEventToState仅触发一次的方方面面,并解答onreachbottom只触发一次常见的疑惑,同时我们还将给您一些技巧,以帮助您实现更有效的addEventListener()和attachEvent的区别、addEventListener与attachEvent、addEventListener与attachEvent区别、addEventListener和attachEvent的区别。
本文目录一览:- mapEventToState仅触发一次(onreachbottom只触发一次)
- addEventListener()和attachEvent的区别
- addEventListener与attachEvent
- addEventListener与attachEvent区别
- addEventListener和attachEvent的区别
mapEventToState仅触发一次(onreachbottom只触发一次)
我在做错什么,我的Bloc模式中的状态仅更改一次mapEventToState
却不响应BlocProvider.of<CounterBloc>(context).add(ActiveEvent());
请求?
我正在尝试使用Bloc模式,但是当我在计数器页面上的切换器中切换状态时,状态会更改,此后它根本不会更新。就像别无选择地onChanged
切换功能。
我猜问题出在我的流订阅中,该订阅是在CounterBloc承包商中实现的。否则我会错误地返回状态。
感谢您的帮助,如果您向我解释错误,我将不胜感激。
我的集团
import ''dart:async'';import ''package:flutter_bloc/flutter_bloc.dart'';import ''package:practicing_bloc/blocs/counter/counterEvents.dart'';import ''package:practicing_bloc/blocs/counter/counterState.dart'';class CounterBloc extends Bloc<CounterEvent, CounterState> { @override CounterState get initialState => Active(active: true, count: 0); CounterBloc() { _counterStream = _counter.stream; } StreamController<CounterState> _counter = StreamController<CounterState>(); Stream<CounterState> _counterStream; @override Stream<CounterState> mapEventToState(CounterEvent event) async* { CounterState currentState = state; print(''currect: $currentState''); if (event is ActiveEvent) { _counter.add(Active(active: true, count: currentState.count)); yield* _counterStream; } else if (event is InactiveEvent) { _counter.add(Inactive(active: false, count: currentState.count)); yield* _counterStream; } }}
集团状态
import ''package:equatable/equatable.dart'';import ''package:meta/meta.dart'';abstract class CounterState extends Equatable { final bool active; final int count; const CounterState({@required this.active, @required this.count}); @override List<Object> get props => [active, count]; @override String toString() => ''State { active : $active, count : $count }'';}class Active extends CounterState { const Active({@required bool active, @required int count}) : super(active: active, count: count);}class Inactive extends CounterState { const Inactive({@required bool active, @required int count}) : super(active: active, count: count);}
团体事件
import ''package:equatable/equatable.dart'';abstract class CounterEvent extends Equatable { const CounterEvent(); @override List<Object> get props => [];}class Increase extends CounterEvent {}class Decrease extends CounterEvent {}class ActiveEvent extends CounterEvent {}class InactiveEvent extends CounterEvent {}
counterPage
import ''package:flutter/material.dart'';import ''package:flutter_bloc/flutter_bloc.dart'';import ''package:practicing_bloc/blocs/counter/counterBloc.dart'';class CounterPage extends StatefulWidget { @override _CounterPageState createState() => _CounterPageState();}class _CounterPageState extends State<CounterPage> { bool stateActive = false; @override Widget build(BuildContext context) { //ignore: close_sinks dynamic counterBloc = BlocProvider.of<CounterBloc>(context); return Scaffold( appBar: AppBar(title: Text(''Flutter Counter | Page title'')), body: SafeArea( child: BlocBuilder<CounterBloc, CounterState>( builder: (context, state) { String stateString = state.active ? ''Active'' : ''Inactive''; return Center( child: Column( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text(''Counter is : $stateString''), Text(''Current counter is : ${state.count}''), Switch( value: stateActive, onChanged: (bool value) { print(counterBloc.state); setState(() { stateActive = value; }); CounterEvent newEvent = value ? ActiveEvent() : InactiveEvent(); counterBloc.add(newEvent); // print(''BloC state: ${counterBloc.state.active} | switch state: ${state.active}''); }, ) ], ), ); }, ), ), ); }}
答案1
小编典典基本上,而不是屈服* _counterStream你需要得到这个IE中的状态Active
或Inactive
改变这个
if (event is ActiveEvent) { _counter.add(Active(active: true, count: currentState.count)); yield* _counterStream; } else if (event is InactiveEvent) { _counter.add(Inactive(active: false, count: currentState.count)); yield* _counterStream; }
对此
if (event is ActiveEvent) { yield Inactive(active: false, count: currentState.count); } else if (event is InactiveEvent) { yield Active(active: true, count: currentState.count); }
addEventListener()和attachEvent的区别
看到这2种事件绑定方法,研究下
attachEvent
attachEvent(event, function,):IE提出的标准
浏览器支持
表格中的数字表示支持该方法的第一个浏览器的版本号。只支持IE
方法 | IE |
---|---|
attachEvent() |
语法
event | 必须。字符串,指定事件名。 取消绑定事件:detachEvent() |
function | 必须。指定要事件触发时执行的函数。 当事件对象会作为第一个参数传入函数。 事件对象的类型取决于特定的事件。例如, "onclick" 事件属于 MouseEvent(鼠标事件) 对象。 |
范例
document.getElementById("myBtn").attachEvent("onclick", myFunction)
//官方的example已经移除,并推荐使用addEventListener
addEventListener
addEventListener(event, function, boolean):W3C提出的标准,建立在DOM Level 2 Events基础上
浏览器支持
表格中的数字表示支持该方法的第一个浏览器的版本号。
方法 | chrome | IE | firefox | sifari | Opera |
---|---|---|---|---|---|
addEventListener() | 1.0 | 9.0 | 1.0 | 1.0 | 7.0 |
语法
event | 必须。字符串,指定事件名。 取消绑定事件:removeEventListener |
function | 必须。指定要事件触发时执行的函数。 当事件对象会作为第一个参数传入函数。 事件对象的类型取决于特定的事件。例如, "click" 事件属于 MouseEvent(鼠标事件) 对象。 |
useCapture | 可选。布尔值,指定事件是否在捕获或冒泡阶段执行。 可能值:
|
范例
document.getElementById("myBtn").addEventListener("click", myFunction)
addEventListener与attachEvent
一、attachEvent和addEventListener
(一)addEventListener
addEventListener() 方法用于向指定元素添加事件句柄。使用 removeEventListener() 方法来移除 addEventListener() 方法添加的事件句柄。
语法:element.addEventListener(event, function, useCapture)
event (必须)字符串,指定事件名。注意: 不要使用 “on” 前缀。 例如,使用 “click” ,而不是使用 “onclick”。
function (必须)指定要事件触发时执行的函数。当事件对象会作为第一个参数传入函数。 事件对象的类型取决于特定的事件。例如, “click” 事件属于 MouseEvent(鼠标事件) 对象。
useCapture (可选)布尔值,指定事件是否在捕获或冒泡阶段执行。【true:事件句柄在捕获阶段执行; false:默认,事件句柄在冒泡阶段执行】
可以添加许多事件,添加的事件不会覆盖已存在的事件。
示例:给button绑定两个点击事件,点击按钮时,两个事件都是执行,执行顺序为:myFunction 、someOtherFunction ,不会覆盖。(当然也可以绑定不同的事件)
var obj = document.getElementById("myBtn")
obj.addEventListener("click", myFunction);
obj.addEventListener("click", someOtherFunction);
示例:使用 removeEventListener() 方法移除由 addEventListener() 方法添加的事件句柄
// 添加 <div> 事件句柄
document.getElementById("myDIV").addEventListener("mousemove", myFunction);
// 移除 <div> 事件句柄
document.getElementById("myDIV").removeEventListener("mousemove", myFunction);
(二)attachEvent
attachEvent方法两个参数:第一个参数为事件名称,第二个参数为接收事件处理的函数;
可以添加许多事件,添加的事件不会覆盖已存在的事件。
二、attachEvent和addEventListener异同点
相同点:
都是dom对象的方法,可以实现一种事件绑定多个事件处理函数。
不同点:
1.attachEvent是IE有的方法,它不遵循W3C标准,而其他的主流浏览器如FF等遵循W3C标准的浏览器都使用addEventListener,所以实际开发中需分开处理。
2.多次绑定后执行的顺序是不一样的,attachEvent是后绑定先执行,addEventListener是先绑定先执行。
三、兼容性
addEventListener——兼容:firefox、chrome、IE、safari、opera;不兼容IE7、IE8
attachEvent——兼容:IE7、IE8;不兼容firefox、chrome、IE9、IE10、IE11、safari、opera
addEventListener与attachEvent区别
DOM2级事件处理程序
DOM2级事件定义了两个方法用于处理指定和删除事件处理程序的操作:
- addEventListener
- removeEventListener
所有的DOM节点都包含这两个方法,并且他们都接受三个参数:
1.事件类型
2.事件处理方法
3.布尔参数,默认false
(true捕获阶段调用事件处理方法;false冒泡阶段调用事件处理方法。)
参数 参数说明 element.addEventListener(type,listener,bool)
element 要绑定事件的对象,及HTML节点。
type 事件名称,注意去掉事件前边的“on”,比如“onclick”要写 成“click”,“onmouseover”要写成“mouseover”。
listener 要绑定的事件监听函数,注意只写函数名,不要带括号。
bool: true捕获阶段调用事件处理方法;false冒泡阶段调用事件处理方法。
IE不支持addEventListener和removeEventListener方法
实现了两个类似的方法:
- attachEvent
- detachEvent
这两个方法都接受两个相同的参数。
1.事件处理程序名称
2.事件处理程序方法
IE只支持事件冒泡
兼容性
attachEvent——兼容:IE7、IE8;不兼容firefox、chrome、IE9、IE10、IE11、safari、opera
addEventListener——兼容:firefox、chrome、IE、safari、opera;不兼容IE7、IE8
兼容IE和非IE浏览器事件绑定的代码:
function on(element, type, callback) { if (element.addEventListener) { // 支持使用 addEventListener() // 判断 type 是否以 "on" 开头 if (type.slice(0,2) === "on") // 以 "on" 开头,不需要,则去掉 type = type.slice(2); element.addEventListener(type, callback); } else { // 不支持使用 addEventListener() // 判断 type 是否以 "on" 开头 if (type.slice(0, 2) !== "on") // 没有以 "on" 开头,需要,则加上 type = "on" + type; element.attachEvent(type, callback); } }
addEventListener和attachEvent的区别
addEventListener的事件处理函数时作为DOM元素的方法来调用,因此this是DOM元素
attachEvent的事件处理函数时作为全局函数来调用,因此this是window
今天关于mapEventToState仅触发一次和onreachbottom只触发一次的讲解已经结束,谢谢您的阅读,如果想了解更多关于addEventListener()和attachEvent的区别、addEventListener与attachEvent、addEventListener与attachEvent区别、addEventListener和attachEvent的区别的相关知识,请在本站搜索。
本文标签: