Dart 如何将数据列表保存到本地存储器
我正在开发Flitter上的电影发现应用程序,我需要保存本地存储上正在播放的电影列表,以供脱机使用,那么我该怎么做呢 Future>getNowPlayingMovies()异步{ 最后的弦乐正在演奏= '' + “$myapikey”+ “&page=”+ "一",Dart 如何将数据列表保存到本地存储器,dart,flutter,Dart,Flutter,我正在开发Flitter上的电影发现应用程序,我需要保存本地存储上正在播放的电影列表,以供脱机使用,那么我该怎么做呢 Future>getNowPlayingMovies()异步{ 最后的弦乐正在演奏= '' + “$myapikey”+ “&page=”+ "一", var httpClient = new HttpClient(); try { // Make the call var request = await httpClient.getUrl(Uri.parse(nowPl
var httpClient = new HttpClient();
try {
// Make the call
var request = await httpClient.getUrl(Uri.parse(nowPlaying));
var response = await request.close();
if (response.statusCode == HttpStatus.OK) {
var jsonResponse = await response.transform(utf8.decoder).join();
// Decode the json response
var data = jsonDecode(jsonResponse);
// Get the result list
List results = data["results"];
print(results);
// Get the Movie list
List<moviemodel> movieList = createNowPlayingMovieList(results);
// Print the results.
return movieList;
} else {
print("Failed http call.");
}
} catch (exception) {
print(exception.toString());
}
return null;}
List<moviemodel> createNowPlayingMovieList(List data) {
List<Searchmodel> list = new List();
for (int i = 0; i < data.length; i++) {
var id = data[i]["id"];
String title = data[i]["name"];
String posterPath = data[i]["poster_path"];
String mediatype = data[i]["media_type"];
moviemodel movie = new moviemodel(id, title, posterPath, mediatype);
list.add(movie);
}
return list; }
List<Widget> createNowPlayingMovieCardItem(
List<moviemodel> movies, BuildContext context) {
// Children list for the list.
List<Widget> listElementWidgetList = new List<Widget>();
if (movies != null) {
var lengthOfList = movies.length;
for (int i = 0; i < lengthOfList; i++) {
Searchmodel movie = movies[i];
// Image URL
var imageURL = "https://image.tmdb.org/t/p/w500/" + movie.posterPath;
// List item created with an image of the poster
var listItem = new Padding(
padding: const EdgeInsets.all(8.0),
child: new Container(
width: 105.0,
height: 155.0,
child: new Column(
children: <Widget>[
new GestureDetector(
onTap: () {
Navigator.push(
context,
new MaterialPageRoute(
builder: (_) => new Detail(movie.id)),
);
},
child: new Container(
width: 105.0,
height: 155.0,
child: new ClipRRect(
borderRadius: new BorderRadius.circular(7.0),
child: new Hero(
tag: movie.title,
child: new FadeInImage.memoryNetwork(
placeholder: kTransparentImage,
image: imageURL,
fit: BoxFit.cover,
),
),
),
decoration: new BoxDecoration(boxShadow: [
new BoxShadow(
color: Colors.black12,
blurRadius: 10.0,
offset: new Offset(0.0, 10.0)),
]),
),
),
new Padding(
padding: const EdgeInsets.only(top: 18.0),
child: new Text(
movie.title,
maxLines: 2,
),
)
],
),
),
);
;
listElementWidgetList.add(listItem);
}
} else {
print("no movie search");
}
return listElementWidgetList;}
var-httpClient=new-httpClient();
试一试{
//打电话
var request=wait-httpClient.getUrl(Uri.parse(nowPlaying));
var response=wait request.close();
if(response.statusCode==HttpStatus.OK){
var jsonResponse=await response.transform(utf8.decoder.join();
//解码json响应
var data=jsonDecode(jsonResponse);
//获取结果列表
列表结果=数据[“结果”];
打印(结果);
//获取电影列表
List movieList=createNowPlayingMovieList(结果);
//打印结果。
回归电影人;
}否则{
打印(“失败的http调用”);
}
}捕获(例外){
打印(exception.toString());
}
返回null;}
列表createNowPlayingMovieList(列表数据){
列表=新列表();
对于(int i=0;i新细节(movie.id)),
);
},
子容器:新容器(
宽度:105.0,
身高:155.0,
子项:新ClipRRect(
边界半径:新边界半径。圆形(7.0),
孩子:新英雄(
标签:movie.title,
孩子:新FadeInImage.memoryNetwork(
占位符:K透明图像,
图片:imageURL,
适合:BoxFit.cover,
),
),
),
装饰:新的BoxEdition(boxShadow:[
新盒影(
颜色:颜色。黑色,
半径:10.0,
偏移量:新偏移量(0.0,10.0)),
]),
),
),
新填料(
填充:仅限常量边集(顶部:18.0),
儿童:新文本(
电影名称,
maxLines:2,
),
)
],
),
),
);
;
添加(listItem);
}
}否则{
打印(“无电影搜索”);
}
返回listElementWidgetList;}
谢谢大家! 使用
路径\u提供程序
:
内容
=“诸如此类”
文件:
文件有很多方法可以帮助您,签出:
更新示例并编写一个空方法用于保存,另一个用于从存储器读取(两者都应为空)。我将回答解决方案。能否在sqlite中存储数据列表?能否在sqlite中存储数据列表?@BhanukaIsuru sure.Can回答我的问题 Future get _localPath async { final directory = await getApplicationDocumentsDirectory(); return directory.path; } Future get _localFile async { final path = await _localPath; return File('$path/yourfile.txt'); } Future writeCounter(int counter) async { final file = await _localFile; // Write the file return file.writeAsString('blah bla blah'); } Future readCounter() async { try { final file = await _localFile; // Read the file String contents = await file.readAsString(); return int.parse(contents); } catch (e) { // If we encounter an error, return 0 return 0; } }