如何将数据从方法 _callLogs 传递到正文并打印出来?
How do I pass data from method _callLogs to body and print it?
我刚开始使用 flutter 框架,我想将调用日志传递到我的应用程序以便打印它们。我该怎么做?
class _QuoteListState extends State<QuoteList> {
Future<bool>_callLogs() async {
Iterable<CallLogEntry> entries = await CallLog.get();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[200],
appBar: AppBar(
title: Text('Calls Log'),
centerTitle: true,
backgroundColor: Colors.purple,
),
body: ListView.builder(
itemCount: entries.length,
itemBuilder: (context, index){
}
)
);
您必须从异步函数中获取数据并设置为局部变量,因此最好在 _callLogs 中调用 setState as 并使您的小部件有状态并按如下方式编写
import 'package:call_log/call_log.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: LogsList(),
));
}
class LogsList extends StatefulWidget {
@override
_LogsListState createState() => _LogsListState();
}
class _LogsListState extends State<LogsList> {
@override
void initState() {
super.initState();
_callLogs();
}
Future<void> _callLogs() async {
Iterable<CallLogEntry> entries = await CallLog.get();
List _data = entries.map((data) => data).toList();
setState(() {
_entries = _data;
});
}
List<CallLogEntry> _entries = [];
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[200],
appBar: AppBar(
title: Text('Calls Log'),
centerTitle: true,
backgroundColor: Colors.purple,
),
body: ListView.builder(
itemCount: _entries.length,
itemBuilder: (context, i) {
final CallLogEntry callLogEntry = _entries[i];
return ListTile(
title: Text(callLogEntry.formattedNumber),
subtitle: Text(callLogEntry.duration.toString()),
);
}),
);
}
}
确保将此添加到您的项目中Android清单:
您可以使用 Future Builder 来实现。
以下最少的代码模拟了您的情况,因此您可以在自己的代码中实施。
import 'package:flutter/material.dart';
class DeleteWidget extends StatefulWidget {
const DeleteWidget({Key key}) : super(key: key);
@override
_DeleteWidgetState createState() => _DeleteWidgetState();
}
class _DeleteWidgetState extends State<DeleteWidget> {
@override
void initState() {
super.initState();
}
List<int> _data;
Future<bool> _callLogs() async {
await Future.delayed(Duration(seconds: 4));
_data = [6, 5, 4, 3, 2, 1];
return true;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Center(
child: FutureBuilder(
future: _callLogs(),
builder: (context, snapshot) {
if (!snapshot.hasError) {
if (snapshot.data == true) {
return ListView.builder(
itemCount: _data.length,
itemBuilder: (context, index) {
return Text(_data[index].toString());
},
);
} else {
return Text("Whatever when it returns false");
}
} else {
return Text("Something wents wrong");
}
},
),
),
),
);
}
}
我刚开始使用 flutter 框架,我想将调用日志传递到我的应用程序以便打印它们。我该怎么做?
class _QuoteListState extends State<QuoteList> {
Future<bool>_callLogs() async {
Iterable<CallLogEntry> entries = await CallLog.get();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[200],
appBar: AppBar(
title: Text('Calls Log'),
centerTitle: true,
backgroundColor: Colors.purple,
),
body: ListView.builder(
itemCount: entries.length,
itemBuilder: (context, index){
}
)
);
您必须从异步函数中获取数据并设置为局部变量,因此最好在 _callLogs 中调用 setState as 并使您的小部件有状态并按如下方式编写
import 'package:call_log/call_log.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: LogsList(),
));
}
class LogsList extends StatefulWidget {
@override
_LogsListState createState() => _LogsListState();
}
class _LogsListState extends State<LogsList> {
@override
void initState() {
super.initState();
_callLogs();
}
Future<void> _callLogs() async {
Iterable<CallLogEntry> entries = await CallLog.get();
List _data = entries.map((data) => data).toList();
setState(() {
_entries = _data;
});
}
List<CallLogEntry> _entries = [];
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[200],
appBar: AppBar(
title: Text('Calls Log'),
centerTitle: true,
backgroundColor: Colors.purple,
),
body: ListView.builder(
itemCount: _entries.length,
itemBuilder: (context, i) {
final CallLogEntry callLogEntry = _entries[i];
return ListTile(
title: Text(callLogEntry.formattedNumber),
subtitle: Text(callLogEntry.duration.toString()),
);
}),
);
}
}
确保将此添加到您的项目中Android清单:
您可以使用 Future Builder 来实现。
以下最少的代码模拟了您的情况,因此您可以在自己的代码中实施。
import 'package:flutter/material.dart';
class DeleteWidget extends StatefulWidget {
const DeleteWidget({Key key}) : super(key: key);
@override
_DeleteWidgetState createState() => _DeleteWidgetState();
}
class _DeleteWidgetState extends State<DeleteWidget> {
@override
void initState() {
super.initState();
}
List<int> _data;
Future<bool> _callLogs() async {
await Future.delayed(Duration(seconds: 4));
_data = [6, 5, 4, 3, 2, 1];
return true;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Center(
child: FutureBuilder(
future: _callLogs(),
builder: (context, snapshot) {
if (!snapshot.hasError) {
if (snapshot.data == true) {
return ListView.builder(
itemCount: _data.length,
itemBuilder: (context, index) {
return Text(_data[index].toString());
},
);
} else {
return Text("Whatever when it returns false");
}
} else {
return Text("Something wents wrong");
}
},
),
),
),
);
}
}