Flutter post HTTP request - flutter

I'm trying to send a post request and then get some response. This is the site: www.reqres.in and the user data https://reqres.in/api/users.
When I press the Button I don't see any text. Posting name and job to an API and receiving name, id, Datetime and job. If I don't use Widget _showData and show the text in the build below text field then I see the Data, but with a lateInitialization error, but I want to show it using the Widget _showData.
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:http_req_advanced/usermodel.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'HTTP Request 2',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var users;
Future<UserModel> createUser(String name, String job) async {
final apiUrl = "https://reqres.in/api/users";
final response =
await http.post(Uri.parse(apiUrl), body: {"name": name, "job": job});
if (response.statusCode == 201) {
users = userModelFromJson(response.body);
} else
throw Exception('Failed to load');
return users;
}
late UserModel user;
final nameController = TextEditingController();
final jobController = TextEditingController();
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text('HTTP Request'),
),
body: Container(
padding: EdgeInsets.all(16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextField(
controller: nameController,
),
TextField(
controller: jobController,
),
//Text(
// "The user ${user.name} ${user.id} is created at ${user.createdAt} with job${user.job}"),
ElevatedButton(
onPressed: () async {
final String name = nameController.text;
final String job = jobController.text;
final UserModel userr = await createUser(name, job);
setState(() {
user = userr;
_showData(user.name, user.job, user.id, user.createdAt);
});
},
child: Text('Make a Request'),
),
],
),
),
),
);
}
Widget _showData(String name, String job, String id, DateTime createdat) {
return Container(
alignment: Alignment.bottomCenter,
child: SizedBox(
height: 32,
child:
Text('The user $name [$id] is created at $createdat with job $job'),
),
);
}
}

Instead of using late initialization:
late UserModel user;
Use:
UserModel? user;
When you use late you are declaring a non null variable that will be later initialized, in this case you don't need to use late because user can be null.

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:http_req_advanced/usermodel.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'HTTP Request 2',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var users;
Future<UserModel> createUser(String name, String job) async {
final apiUrl = "https://reqres.in/api/users";
final response =
await http.post(Uri.parse(apiUrl), body: {"name": name, "job": job});
if (response.statusCode == 201) {
users = userModelFromJson(response.body);
} else
throw Exception('Failed to load');
return users;
}
late UserModel user;
final nameController = TextEditingController();
final jobController = TextEditingController();
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text('HTTP Request'),
),
body: Container(
padding: EdgeInsets.all(16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextField(
controller: nameController,
),
TextField(
controller: jobController,
),
user != null
? _showData(user.name, user.job, user.id, user.createdAt)
: Container(),
//Text(
// "The user ${user.name} ${user.id} is created at ${user.createdAt} with job${user.job}"),
ElevatedButton(
onPressed: () async {
final String name = nameController.text;
final String job = jobController.text;
final UserModel userr = await createUser(name, job);
setState(() {
user = userr;
});
},
child: Text('Make a Request'),
),
],
),
),
),
);
}
Widget _showData(String name, String job, String id, DateTime createdat) {
return Container(
alignment: Alignment.bottomCenter,
child: SizedBox(
height: 32,
child:
Text('The user $name [$id] is created at $createdat with job $job'),
),
);
}
}

Related

I have a problem with the search implementation

in this code a have a search function but when ever I search for apple its shows me apple a few times instead of only once
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
MyHomePage({Key key, #required this.title}) : super(key: key);
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
//Search TextField Controller
final _searchController = TextEditingController();
List<Fruit> mainList = [
Fruit(name: 'Apple', imageUrl: 'https://images.pexels.com/photos/102104/pexels-photo-102104.jpeg'),
Fruit(name: 'Banana', imageUrl: 'https://images.pexels.com/photos/5945848/pexels-photo-5945848.jpeg'),
Fruit(name: 'Pineapple', imageUrl: 'https://images.pexels.com/photos/1071878/pexels-photo-1071878.jpeg'),
Fruit(name: 'Mango', imageUrl: 'https://images.pexels.com/photos/918643/pexels-photo-918643.jpeg'),
];
List<Fruit> searchList = [];
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
children: [
Container(
width: MediaQuery.of(context).size.width,
height: 60.0,
child: TextFormField(
controller: _searchController,
onChanged: (text){
final String queryString = _searchController.text;
setState((){
if(queryString.isNotEmpty){
for(final fruit in mainList){
if(fruit.name.contains(queryString)){
searchList.add(fruit);
} else{
searchList.remove(fruit);
}
}
}else{
searchList.clear();
}
});
}
),
),
Expanded(
child: _searchController.text.isEmpty
? GridView.count(
crossAxisCount: 2,
children: mainList.map((fruit)=> CardWidget(fruit: fruit)).toList(),
)
:GridView.count(
crossAxisCount: 2,
children: searchList.map((searchedFruit)=>CardWidget(fruit: searchedFruit)).toList()
),
),
],
),
);
}
}
I think that the main problem here is with the searchList, It just add the same fruit over and over.
Class to hold Fruit
class Fruit{
final String imageUrl;
final String name;
Fruit({this.imageUrl, this.name});
}
widget to be built for each fruit object found in the mainList
//Card Widget
class CardWidget extends StatefulWidget{
final Fruit fruit;
CardWidget({this.fruit});
#override
_CardWidgetState createState()=> _CardWidgetState();
}
class _CardWidgetState extends State<CardWidget>{
#override
Widget build(BuildContext context){
return Container(
width: 100.0,
height: 140.0,
child: Column(
children:[
Image(image: NetworkImage(widget.fruit.imageUrl)),
SizedBox(height: 10.0),
Text(widget.fruit.name),
]
)
);
}
}
Ok, I believe the problem is every time you enter a new character the code will search and add to the search list, but you have to reset the list for each search otherwise the list will have repeated characters. Here's how to do it:
onChanged: (text){
final String queryString = _searchController.text;
setState((){
if(queryString.isNotEmpty){
searchList.clear(); // clear the list before the search loop
for(final fruit in mainList){
if(fruit.name.contains(queryString)){
searchList.add(fruit);
} else{
searchList.remove(fruit);
}
}
}else{
searchList.clear();
}
}
try this..
initialize debounce below the TextEditingController
final _searchController = TextEditingController();
final _deBouncer = Debouncer(millisecound: 400);
replace onChanged function
onChanged: (text) async {
searchList.clear();
await _deBouncer.run(() {
final String queryString = text;
if (queryString.isNotEmpty) {
for (final fruit in mainList) {
if
(fruit.name?.toLowerCase().contains(queryString.toLowerCase()) ??
false) {
searchList.add(fruit);
}
}
}
setState(() {});
});
}),
add this class for when user stop to typing in search text field
class Debouncer {
final int? millisecound;
VoidCallback? action;
Timer? _timer;
Debouncer({this.millisecound});
run(VoidCallback action) {
if (_timer != null) {
_timer?.cancel();
}
_timer = Timer(Duration(milliseconds: millisecound ?? 300), action);
}
}

shared_preferences Flutter Add items

I expect that when I click the button I will add a single item that has brand and date in it in the Save Data function however I have no idea how to do that.Currently after each click they add tems however they are multiplied by themselves and that completely misses the point . I tried to add a single item however I get information about unsupported Error . In conclusion Please help
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'SharedPreferences Demo',
home: Test(),
);
}
}
class Test extends StatefulWidget {
const Test({Key? key}) : super(key: key);
#override
State<Test> createState() => _TestState();
}
class _TestState extends State<Test> {
List<Map<String, dynamic>> listItems = [];
List<String> cart = [];
final Map<String, dynamic> item = Map<String, String>();
saveData() async {
var date = DateTime.now();
print('save data ');
final Map<String, dynamic> item = Map<String, dynamic>();
item['brand'] = 'item';
item['date'] = 'date';
SharedPreferences prefs = await SharedPreferences.getInstance();
if (cart == null) cart = [];
setState(() {
cart = prefs.getStringList("cart") ?? [];
cart.add(jsonEncode(item));
listItems.add(jsonDecode(item));
//<-- when i click button i want add one item card. on but idk how
prefs.setStringList("cart", cart);
});
}
getUser() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
var cart = prefs.getStringList("cart");
setState(() {
if (cart == null) {
print('null');
} else {
cart.forEach((item) {
listItems.add(jsonDecode(item));
});
}
});
}
#override
void initState() {
super.initState();
getUser();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: <Widget>[
Expanded(
child: listItems.isNotEmpty
? ListView.builder(
itemCount: listItems.length, itemBuilder: buildList)
: Center(
child: Text('EMPTY LIST'),
)),
Center(
child: FloatingActionButton(
onPressed: saveData,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
)
],
),
);
}
Widget buildList(BuildContext context, int index) {
return Container(
margin: EdgeInsets.all(4),
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
width: 2,
),
borderRadius: BorderRadius.circular(5)),
child: ListTile(
title: Text(listItems[index]['brand']),
subtitle: Text(listItems[index]['date'] ?? "null"),
),
);
}
}
}

WebSocket in Dart doesn't return echo messages

I'm trying to make simple echo WebSocket in Dart with remote server, but it's not work (I don't get echo messages back to me). There is no compile errors or logs. No issues with Flutter Doctor. Rebuild doesn't help.
I could find only outdated examples for localhost and not for server.
Both machines are on the same network and can see each other.
server app code:
import 'dart:io';
void main() async {
HttpServer server = await HttpServer.bind('localhost', 8082);
server.transform(WebSocketTransformer()).listen(onWebSocketData);
}
void onWebSocketData(WebSocket client){
client.listen((data) {
client.add('Echo: $data');
});
}
echo app code:
import 'package:web_socket_channel/web_socket_channel.dart';
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
const title = 'WebSocket Demo';
return const MaterialApp(
title: title,
home: MyHomePage(
title: title,
),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({
Key? key,
required this.title,
}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final TextEditingController _controller = TextEditingController();
final _channel = WebSocketChannel.connect(
Uri.parse('wss://172.22.185.10:8082'),
);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Form(
child: TextFormField(
controller: _controller,
decoration: const InputDecoration(labelText: 'Send a message'),
),
),
const SizedBox(height: 24),
StreamBuilder(
stream: _channel.stream,
builder: (context, snapshot) {
return Text(snapshot.hasData ? '${snapshot.data}' : '');
},
)
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _sendMessage,
tooltip: 'Send message',
child: const Icon(Icons.send),
),
);
}
void _sendMessage() {
if (_controller.text.isNotEmpty) {
_channel.sink.add(_controller.text);
}
}
#override
void dispose() {
_channel.sink.close();
_controller.dispose();
super.dispose();
}
}
I was not able to find any detailed documentation or up-to-date answers, so...
Please help me if you can <3
For server shelf package used
shelf_web_socket: ^1.0.1
Client is mentioned in flutter documentation
Client.dart
import 'package:web_socket_channel/web_socket_channel.dart';
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
const title = 'WebSocket Demo';
return const MaterialApp(
title: title,
home: MyHomePage(
title: title,
),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({
Key? key,
required this.title,
}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final TextEditingController _controller = TextEditingController();
final _channel = WebSocketChannel.connect(
Uri.parse('ws://localhost:9001'),
);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Form(
child: TextFormField(
controller: _controller,
decoration: const InputDecoration(labelText: 'Send a message'),
),
),
const SizedBox(height: 24),
StreamBuilder(
stream: _channel.stream,
builder: (context, snapshot) {
return Text(snapshot.hasData ? '${snapshot.data}' : '');
},
)
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _sendMessage,
tooltip: 'Send message',
child: const Icon(Icons.send),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
void _sendMessage() {
if (_controller.text.isNotEmpty) {
_channel.sink.add(_controller.text);
}
}
#override
void dispose() {
_channel.sink.close();
_controller.dispose();
super.dispose();
}
}
Server.dart
import 'package:shelf/shelf_io.dart' as shelf_io;
import 'package:shelf_web_socket/shelf_web_socket.dart';
void main() {
var handler = webSocketHandler((webSocket) {
webSocket.stream.listen((message) {
webSocket.sink.add("echo $message");
});
});
shelf_io.serve(handler, 'localhost', 9001).then((server) {
print('Serving at ws://${server.address.host}:${server.port}');
});
}
Remote Accessing
Remote accessing not a complicated
in your server side has a firewall . so you need to allow connection to this port( here 9001) for outside acces.Set your server ip in the code instead of localhost in both side like client and server.
Allow port in firewall on windows Os
1
2
3
4
5
6
7
Done.now you can access the port outside.

accessing a private method from another class flutter

I'm new to flutter.I have here 3 classes which are the Login(), MainMenu() which is the screen after already logged, and this MyDrawer()which is a drawer of my App.
What I'm trying to do is I want to access the signOut() method from Login(). How would I do it or what should I do to redesign my code. I've tried below accessing it and it receives and exception The method 'call' was called on null.
This is a code snippet from my full code:
class Login extends StatefulWidget {
#override
_LoginState createState() => _LoginState();
}
enum LoginStatus { notSignIn, signIn }
class _LoginState extends State<Login> {
LoginStatus _loginStatus = LoginStatus.notSignIn;
String email, password;
final _key = new GlobalKey<FormState>();
bool _secureText = true;
signOut() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
setState(() {
_loginStatus = LoginStatus.notSignIn;
});
}
#override
void initState() {
super.initState();
}
#override
Widget build(BuildContext context) {
switch (_loginStatus) {
case LoginStatus.notSignIn:
return Scaffold(
backgroundColor: Colors.cyan,
body: Center(
child: ListView(
shrinkWrap: true,
padding: EdgeInsets.all(15.0),
children: <Widget>[
Center(
child: Container(
padding: const EdgeInsets.all(8.0),
color: Colors.cyan,
child: Form(
key: _key,
break;
case LoginStatus.signIn:
return MainMenu();
break;
}
}
}
class MainMenu extends StatefulWidget {
#override
_MainMenuState createState() => _MainMenuState();
}
class _MainMenuState extends State<MainMenu> {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
)
)
}
class MyDrawer extends StatefulWidget {
final Function onTap;
final VoidCallback signOut;
MyDrawer(
{this.onTap,this.signOut
});
#override
_MyDrawerState createState() => _MyDrawerState();
}
class _MyDrawerState extends State<MyDrawer> {
signOut() {
setState(() {
widget.signOut();
});
}
#override
void initState() {
super.initState();
}
#override
Widget build(BuildContext context) {
return SizedBox(
width: MediaQuery
.of(context)
.size
.width * 0.7,
child: Drawer(
child: Container(
height:100,
color: Colors.white,
child: ListView(
padding: EdgeInsets.all(0),
children: <Widget>[
ListTile(
leading: Icon(Icons.exit_to_app,color: Colors.cyan, size: 30.0),
onTap: () {
signOut();
},
title: Text("Logout",
style: TextStyle(color: Colors.black,fontWeight: FontWeight.w500, fontSize: 18),
),
),
],
),
),
),
);
}
}
I'm really stuck with this problem. Any help would be greatly appreciated. Thanks!

Flutter how to pass array and show in other widget

I have a list of arrays i need to pass it to the other stateful widget and show the array there
This is my function code which retrieve data from API
Future<List> doSomeAsyncStuff() async {
final storage = new FlutterSecureStorage();
String value = await storage.read(key: 'Cnic');
print(value);
String url = 'http://api.php?offset=0&limit=1&cnic=${value}' ;
final msg = jsonEncode({"cnic": value});
Map<String,String> headers = {'Content-Type':'application/json'};
String token = value;
final response = await http.get(url);
var Data = json.decode(response.body);
print(Data);
var familyMembers = Data["records"][0]["family_members"];
print(familyMembers);
for (var familyMember in familyMembers){ //prints the name of each family member
print(familyMember["name"]);
print(familyMember["gender"]);
}
}
As you can see there is 2 list familyMember["name"] and familyMember["gender"] i need to pass it to statefulwidget
I am simple passing it like this
Navigator.push(
context,
MaterialPageRoute(builder: (context) => PersonalPage(familyMember["name"], familyMember["gender"])),
);
This is my other stateful widget I need to show the array of name and gender here
import 'package:flutter/material.dart';
class PersonalPage extends StatefulWidget {
final String name;
final String gender;
PersonalPage(this.name, this.gender);
#override
_PersonalPageState createState() => _PersonalPageState();
}
class _PersonalPageState extends State<PersonalPage> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text('I need to print name and gender here ')
),
);
}
}
flut
Try this and change your code as per this: As your First Page code is missing I have created a dummy forst Page.
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
title: 'My APP',
home: FirstRoute(),
));
}
class FirstRoute extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('List of ....'),
),
body: Center(
child: RaisedButton(
child: Text('Open details'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => PersonalPage("NAME","GENDER")),
);
},
),
),
);
}
}
class PersonalPage extends StatefulWidget {
final String name;
final String gender;
PersonalPage(this.name, this.gender);
#override
_PersonalPageState createState() => _PersonalPageState();
}
class _PersonalPageState extends State<PersonalPage> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Row(
children : [
Text(widget.name),
Text(widget.gender),
]
)
),
);
}
}
You are doing wrong you are passing list and in stateful widget you mention its a string you can do something like this
List<String> familyMemberName = [];
List<String> familyMemberGender = [];
Future<List> doSomeAsyncStuff() async {
final storage = new FlutterSecureStorage();
String value = await storage.read(key: 'Cnic');
print(value);
String url = 'http://api.php' ;
final msg = jsonEncode({"cnic": value});
Map<String,String> headers = {'Content-Type':'application/json'};
String token = value;
final response = await http.get(url);
var Data = json.decode(response.body);
print(Data);
var familyMembers = Data["records"][0]["family_members"];
print(familyMembers);
for (var familyMember in familyMembers){
familyMemberName.add(familyMember["name"]);
familyMemberGender.add(familyMember["gender"]);
print(familyMemberName);
}
}
and in you personal widget like this
import "package:flutter/material.dart";
class PersonalPage extends StatefulWidget {
final List<String> names;
final List<String> relation;
PersonalPage(this.names,this.relation);
#override
_PersonalPageState createState() => _PersonalPageState();
}
class _PersonalPageState extends State<PersonalPage> {
#override
Widget build(BuildContext context) {
return Scaffold(
body:
ListView.builder( //use ListView here to show all the names and genders
itemCount: widget.names.length,
itemBuilder: (BuildContext context,int index){
return Padding(
padding: EdgeInsets.only(left: 10, right: 10, bottom: 10, top: 10),
child: Card(
child: Padding(
padding: EdgeInsets.all(5),
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Text('Name:', style: TextStyle(color: Colors.blue, fontWeight: FontWeight.bold),),
Text(widget.names[index])
],
),
Row(
children: <Widget>[
Text('Gender:', style: TextStyle(color: Colors.blue, fontWeight: FontWeight.bold),),
Text(widget.genders[index])
],
),
],
),
),
),
);
})
);
}
}
Sorry i test code so thats why i add it in card
Text(widget.name)
Text(widget.gender)
or
Text("My name is ${widget.name} and my gender is ${widget.gender}")