Reputation: 41
I have below code and it's giving me a warning as below and during runtime it says A build function returned null.
This function has a return type of 'Widget', but doesn't end with a return statement. Try adding a return statement, or changing the return type to 'void'.
UPDATE: What's wrong in below code .?.
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");
}
}),
],
),
),
),
);
}
}
Upvotes: 1
Views: 917
Reputation: 12803
This function has a return type of 'Widget', but doesn't end with a return statement. Try adding a return statement, or changing the return type to 'void'.
The warning told you everything. You have two if
there, so you need to have two else
too.
if (snapshot.hasData) {
if (snapshot.data != null) {
...
}else{
return Text("It is null");
}
}else{
return Text("Empty");
}
Upvotes: 1