-
-
Save renefloor/2a1593a2431306093d707cbccacb4e92 to your computer and use it in GitHub Desktop.
Test try-catch on cachemanager.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_cache_manager/flutter_cache_manager.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Demo', | |
home: Scaffold( | |
appBar: AppBar(title: Text('demo')), | |
body: TestWidget(), | |
), | |
); | |
} | |
} | |
class TestWidget extends StatefulWidget { | |
@override | |
_TestWidgetState createState() => _TestWidgetState(); | |
} | |
class _TestWidgetState extends State<TestWidget> { | |
String imageName = "loading..."; | |
@override | |
void initState() { | |
getFile().then((file) { | |
setState(() { | |
if(file == null){ | |
imageName = 'loading failed'; | |
}else{ | |
imageName = file.path.split('/').last; | |
} | |
}); | |
}); | |
super.initState(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Center( | |
child: Text(imageName), | |
); | |
} | |
Future<File> getFile() async { | |
var url = 'https://notworkingtesturl.com/not-an-image'; | |
try { | |
var file = await DefaultCacheManager().getSingleFile(url); | |
return file; | |
} catch (e) { | |
print(e); | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment