Last active
September 5, 2021 00:19
-
-
Save desi-programmer/657c07b22744ed9415feb59477768020 to your computer and use it in GitHub Desktop.
Simple Code Files for Flutter Node JS Pagination
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
const express = require('express'); | |
const router = express.Router(); | |
const Ideas = require('../model/ideas'); | |
// The Idea Model for reference | |
// const mongoose = require('mongoose'); | |
// const ideaSchema = new mongoose.Schema({ | |
// owned : { | |
// type : String, | |
// }, | |
// title: { | |
// type: String, | |
// required: true, | |
// }, | |
// description: { | |
// type: String, | |
// required: true, | |
// }, | |
// created: { | |
// type: Date, | |
// default: () => Date.now(), | |
// }, | |
// }); | |
// module.exports = mongoose.model('idea', ideaSchema); | |
router.get('/', async (req, res) => { | |
const { page = 1 } = req.query; | |
const limit = 5; | |
// core data , total , current | |
try { | |
const ideas = await Ideas.find().limit(limit).skip((page - 1) * limit).exec(); | |
const count = await Ideas.countDocuments(); | |
res.json({ | |
ideas, | |
totalPages : Math.ceil(count / limit), | |
currenPage : Number(page), | |
}); | |
} catch (error) { | |
res.sendStatus(500).json({'msg' : 'Server Error'}); | |
} | |
}); | |
module.exports = router; |
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:convert'; | |
import 'package:consumer/future_infinite_scroll.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:http/http.dart' as http; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Consumer', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({Key? key}) : super(key: key); | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
int currentPage = 1; | |
Future<Map> getPosts() async { | |
try { | |
var response = await http.get( | |
Uri.parse( | |
"http://10.0.2.2:8000/apis?page=${currentPage}", | |
), | |
); | |
if (response.statusCode == 200) { | |
// return a decoded body | |
return jsonDecode(response.body); | |
} else { | |
// server error | |
return Future.error("Server Error !"); | |
} | |
} catch (SocketException) { | |
// fetching error | |
// may be timeout, no internet or dns not resolved | |
return Future.error("Error Fetching Data !"); | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text( | |
"Pagination", | |
), | |
), | |
// | |
body: SingleChildScrollView( | |
child: FutureBuilder<Map>( | |
future: getPosts(), | |
builder: (context, snapshot) { | |
if (snapshot.hasData) { | |
print(snapshot.data); | |
return Column( | |
children: [ | |
ListView.builder( | |
shrinkWrap: true, | |
physics: NeverScrollableScrollPhysics(), | |
itemCount: snapshot.data!['ideas'].length, | |
itemBuilder: (context, index) { | |
return Container( | |
margin: EdgeInsets.all( | |
15.0, | |
), | |
padding: EdgeInsets.all( | |
15.0, | |
), | |
decoration: BoxDecoration( | |
borderRadius: BorderRadius.circular( | |
5.0, | |
), | |
border: Border.all( | |
color: Colors.black, | |
width: 2.0, | |
), | |
), | |
child: Column( | |
children: [ | |
Text( | |
"${snapshot.data!['ideas'][index]['title']}", | |
style: TextStyle( | |
fontSize: 24.0, | |
), | |
), | |
// | |
Text( | |
"${snapshot.data!['ideas'][index]['description']}", | |
style: TextStyle( | |
fontSize: 18.0, | |
), | |
), | |
// | |
Text( | |
DateTime.parse(snapshot.data!['ideas'][index] | |
['created']) | |
.toString(), | |
style: TextStyle( | |
fontSize: 18.0, | |
), | |
), | |
], | |
), | |
); | |
}, | |
), | |
// | |
// | |
// | |
Row( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
if (snapshot.data!['currenPage'] < | |
snapshot.data!['totalPages']) | |
OutlinedButton( | |
onPressed: () { | |
setState(() { | |
currentPage++; | |
}); | |
}, | |
child: Text( | |
"Next", | |
), | |
), | |
if (snapshot.data!['currenPage'] > 1) | |
OutlinedButton( | |
onPressed: () { | |
setState(() { | |
currentPage--; | |
}); | |
}, | |
child: Text( | |
"Previous", | |
), | |
), | |
], | |
) | |
], | |
); | |
} else if (snapshot.hasError) { | |
return Text(snapshot.error.toString()); | |
} else { | |
return CircularProgressIndicator(); | |
} | |
}, | |
), | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment