我正在听PageController来获取位置,然后将其与ListView同步。当操纵PageView时,将同时操纵ListView。
示例:https://github.com/Ramotion/cardslider-android
但是,在v0.6.0之后,我收到一个断言错误,即我的ScrollController没有附加到任何视图。每当有流事件触发.jumpTo()
方法时,就会触发此事件。仍然可以,但是断言错误使我发疯。
[VERBOSE-2:shell.cc(181)] Dart Error: Unhandled exception:
'package:flutter/src/widgets/scroll_controller.dart': Failed assertion: line 169 pos 12: '_positions.isNotEmpty': ScrollController not attached to any scroll views.
#0 _AssertionError._doThrowNew (dart:core/runtime/liberrors_patch.dart:40:39)
#1 _AssertionError._throwNew (dart:core/runtime/liberrors_patch.dart:36:5)
#2 ScrollController.jumpTo (package:flutter/src/widgets/scroll_controller.dart:169:12)
#3 MyTitle.build.<anonymous closure> (file:///Users/lukepighetti/code/when-coin/when_coin_2/lib/screens/rate/widgets/title.dart:19:19)
#4 _RootZone.runUnaryGuarded (dart:async/zone.dart:1314:10)
#5 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
#6 _DelayedData.perform (dart:async/stream_impl.dart:584:14)
#7 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:700:11)
#8 _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:660:7)
#9 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
#10 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
如何使用ScrollController.jumpTo()
而不遇到此异常?
class MyTitle extends StatelessWidget {
final List<Category> categories;
MyTitle({this.categories});
@override
Widget build(BuildContext context) {
final _controller = ScrollController();
double height = 36.0;
// double width = MediaQuery.of(context).size.width * 0.8;
BlocProvider.of(context).page.listen((page) {
_controller.jumpTo(height * page);
});
return Container(
height: height,
// width: width,
child: ListView(
controller: _controller,
scrollDirection: Axis.vertical,
physics: NeverScrollableScrollPhysics(),
children: categories
.map((c) => _Title(
title: c.title,
index: categories.indexOf(c),
))
.toList(),
),
);
}
}
答案 0 :(得分:3)
检查控制器具有客户端蚂蚁,然后延迟跳转:
if (_scrollController.hasClients) {
Future.delayed(Duration(milliseconds: 50), () {
_scrollController?.jumpTo(_scrollController.position.maxScrollExtent);
});
}
答案 1 :(得分:3)
问题是您正在重新创建const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'mytestingdb';
const retrieveCustomers = (db, callback)=>{
// Get the customers collection
const collection = db.collection('customers');
// Find some customers
collection.find({}).toArray((err, customers) =>{
if(err) throw err;
console.log("Found the following records");
console.log(customers)
callback(customers);
});
}
const retrieveCustomer = (db, callback)=>{
// Get the customers collection
const collection = db.collection('customers');
// Find some customers
collection.find({'name': 'mahendra'}).toArray((err, customers) =>{
if(err) throw err;
console.log("Found the following records");
console.log(customers)
callback(customers);
});
}
const insertCustomers = (db, callback)=> {
// Get the customers collection
const collection = db.collection('customers');
const dataArray = [{name : 'mahendra'}, {name :'divit'}, {name : 'aryan'} ];
// Insert some customers
collection.insertMany(dataArray, (err, result)=> {
if(err) throw err;
console.log("Inserted 3 customers into the collection");
callback(result);
});
}
// Use connect method to connect to the server
MongoClient.connect(url,{ useUnifiedTopology: true }, (err, client) => {
console.log("Connected successfully to server");
const db = client.db(dbName);
insertCustomers(db, ()=> {
retrieveCustomers(db, ()=> {
retrieveCustomer(db, ()=> {
client.close();
});
});
});
});
并在每个_controller
上订阅它。必须将控制器作为最终的类作用域属性创建一次。此外,您应该使用build
到StatefulWidget
控制器并订阅以dispose
方法流式传输。
initState
答案 2 :(得分:1)
作为我上面回答的答案,您正在使用尚未附加到ListView或其他ScrollView的ScrollController。您可以使用hasClients属性进行检查。
if (_scrollController.hasClients) {
await _scrollController.animateTo(
0.0,
curve: Curves.easeOut,
duration: const Duration(milliseconds: 300),
);
}
答案 3 :(得分:-1)
您正在尝试使用scrollController
进行跳转,然后再将scrollController
添加到ScrollView
(列表视图)中。添加到控制器后,我们必须跳转。请参考下面的代码。
// task1
Future.delayed(Duration.zero, () =>
{ // task2
BlocProvider.of(context).page.listen((page) { _controller.jumpTo(height * page); });
});
// task3
这与DispatchQueue.main.async
非常相似,因为持续时间为零。执行顺序为task1,task3,task2