我有下面的代码,它向我发出如下警告,并在运行时显示 build函数返回null。
此函数的返回类型为“窗口小部件”,但不以return语句结尾。 尝试添加return语句,或将返回类型更改为'void'。
更新: 下面的代码有什么问题?..
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:io';
import 'package:path/path.dart';
import 'package:permission_handler/permission_handler.dart';
List<FileSystemEntity> _pdf = [];
class BrowserScaffold extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return TabsApp();
}
}
Future<List> loadfiles() async {
_pdf = [];
int filecount = 0;
var status = await Permission.storage.status;
if (status.isUndetermined) {
await [
Permission.storage,
].request();
}
Directory dir = Directory('/storage/emulated/0/');
String pdfpath = dir.toString();
print('PATH IS ' + pdfpath);
List<FileSystemEntity> _files;
_files = dir.listSync(recursive: true, followLinks: false);
for (FileSystemEntity entity in _files) {
String path = entity.path;
if (path.endsWith('.pdf')) _pdf.add(entity);
}
for (var i = 0; i < _pdf.length; i++) {
//print(_pdf[i]);
}
filecount = _pdf.length;
print('#############ENTERED');
print(filecount);
return _pdf;
}
class TabsApp extends State<BrowserScaffold> {
@override
Widget build(BuildContext context) {
return Container(
child: DefaultTabController(
length: 2,
child: Scaffold(
appBar: AppBar(
title: Text('MY Files'),
bottom: TabBar(tabs: [
Tab(text: 'ALL FILES'),
Tab(text: 'RECENT FILES'),
]),
),
body: TabBarView(
children: [
RaisedButton(
child: Text('LIST FILES'),
onPressed: () => loadfiles(),
),
FutureBuilder(
future: loadfiles(),
builder: (context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
print('OKOK##################################');
if (snapshot.data != null) {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
return Container(
child: Card(
child: Text(
basename(snapshot.data[index].path),
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18),
),
));
//return Text(snapshot.data[index].path);
});
} else {
print('FAIL##################################');
return new CircularProgressIndicator();
}
} else {
print('FAIL2##################################');
return Text("Empty");
}
}),
],
),
),
),
);
}
}
答案 0 :(得分:0)
此函数的返回类型为“小部件”,但不以结束 返回声明。尝试添加return语句,或更改 将类型返回为“ void”。
警告告诉了您一切。您在那里有两个if
,因此您也需要两个else
。
if (snapshot.hasData) {
if (snapshot.data != null) {
...
}else{
return Text("It is null");
}
}else{
return Text("Empty");
}