我正在尝试创建所有扫描设备的列表并显示在ListTile中。
这是我的代码
import difflib
import sys
string1 = """abc
willberemoved
def
willbechanged
ghi
jkl
"""
string2 = """abc
def
changed
ghi
hasbeenadded
jkl
"""
lines1 = string1.splitlines(keepends=True)
lines2 = string2.splitlines(keepends=True)
diff_empty = False
udiff = difflib.unified_diff(lines1, lines2)
try:
next(udiff)
except StopIteration:
diff_empty = True
if not diff_empty:
diff = difflib.ndiff(lines1, lines2)
if diff is not None:
sys.stdout.writelines(diff)
我正在一次获取数据,并且列表每次都在更新。
class HomePageState extends State<Home>{
BluetoothDevice device;
FlutterBlue flutterBlue = FlutterBlue.instance;
List<AdvertiseData> dta = [];
Future<List<AdvertiseData>> getAdvData() async{
var beaconName;
await flutterBlue.scan(timeout: Duration(seconds: 100)).listen((scanResult) {
device = scanResult.device;
beaconName = device.name;
if(beaconName != ""){
AdvertiseData d = AdvertiseData(beaconName, 1);
dta.add(d);
print(dta.length);
}
});
}
我想要的只是一个列表(或最后一个列表),以便我可以在ListTile中显示数据。
答案 0 :(得分:0)
只要您不停止使用FlutterBlue.instance.stopScan()进行扫描,它将继续更新列表。这是正常现象,只知道如何处理。
import 'package:flutter/material.dart';
import 'package:flutter_blue/flutter_blue.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Blue'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool scanning = false;
final _scaffoldKey = GlobalKey<ScaffoldState>();
BluetoothDevice device;
FlutterBlue flutterBlue = FlutterBlue.instance;
List<ScanResult> dta = [];
startStop() {
if (scanning) {
flutterBlue.stopScan();
_scaffoldKey.currentState.showSnackBar(SnackBar(
content: Text("you have ${dta.length} devices"),
));
} else {
flutterBlue.startScan(timeout: Duration(seconds: 4));
}
setState(() {
scanning = !scanning;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(
title: Text(widget.title),
actions: <Widget>[
FlatButton(
child: Text(
scanning ? "Stop Scanning" : "Start Scanning",
style: TextStyle(color: Colors.white, fontSize: 14),
),
onPressed: () {
startStop();
},
)
],
),
body: StreamBuilder<List<ScanResult>>(
stream: FlutterBlue.instance.scanResults,
initialData: [],
builder: (c, snapshot) {
dta = snapshot.data;
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
return Text(snapshot.data[index].device.name);
},
);
},
),
);
}
}