标签:int size gac lap ges port test nal response
import ‘package:flutter/material.dart‘;
import ‘package:http/http.dart‘ as http;
import ‘dart:convert‘;
import ‘package:demo0506/SqliteHelper.dart‘;
void main()=>runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final String url=‘http://jsonplaceholder.typicode.com/posts‘;
final sqlHelp=SqliteHelper();
getAllPost()async{
await sqlHelp.open();
return await sqlHelp.queryAll();
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
child: Icon(Icons.http),
onPressed: ()async{
await sqlHelp.open();
var response = await http.get(url);
List listData = jsonDecode(response.body);
listData.forEach((e) async => await sqlHelp.insert(e));
setState(() {
});
}),
appBar: AppBar(title: Text(‘DataBase‘),),
body: FutureBuilder(
future: getAllPost(),
builder: (BuildContext context,snapshot){
if(snapshot.hasData){
List _listData=snapshot.data;
return ListView.builder(
itemCount: _listData.length,
itemBuilder: (context,index){
return InkWell(
onTap:()async{
await sqlHelp.delete(_listData[index][‘id‘]);
setState(() {
});
},
child: ListTile(title: Text(_listData[index][‘title‘]),));
},);
}
return Container();
}
),
);
}
}
import ‘package:sqflite/sqflite.dart‘;
import ‘package:flutter/material.dart‘;
import ‘package:http/http.dart‘ as http ;
class SqliteHelper{
final sqlFileName=‘mydb.sql‘;
final table=‘post‘;
Database db;
open()async{
String path = ‘${await getDatabasesPath()}/$sqlFileName‘;
if(db==null){
db=await openDatabase(path,version: 1,onCreate: (db,ver)async{
await db.execute(‘‘‘
Create Table post(
id integer primary key,
userId integer,
title text,
body text
);
‘‘‘);
});
}
}
insert(Map<String, dynamic> map)async{
await db.insert(table, map);
}
queryAll()async{
return await db.query(table,columns: null);
}
delete(int id)async{
return await db.delete(table,where: ‘id=$id‘);
}
}
标签:int size gac lap ges port test nal response
原文地址:https://www.cnblogs.com/braveheart007/p/10822750.html