Can a flutter variable be used in a php url - android

I want to use the value of a flutter variable in PHP URL.what can I do to pass the value of the variable.
I've tried passing value or can say a id in a web view in flutter but it is printing the string not the value of the variable.
`
String url = "http://10.0.2.2/pre_beta_02/dummy.php?therapist_id=value";
class NextPage extends StatefulWidget{
//List list;
String value;
//NextPage({this.value});
NextPage({Key key,this.value}):super (key: key);
#override
_Nextpagestate createState() => new _Nextpagestate();
}
class _Nextpagestate extends State<NextPage>{
#override
Widget build(BuildContext context) {
return WebviewScaffold(
appBar: new AppBar(
title: new Text('${widget.value}'),
),
url: url,
);
}
}
`
I expect the output as "value = 5" but the actual output coming is "value"

This should do it:
return WebviewScaffold(
appBar: new AppBar(
title: new Text('${widget.value}'),
),
url: "http://10.0.2.2/pre_beta_02/dummy.php?therapist_id=${widget.value}",
);

Related

type 'List<dynamic>' is not a subtype of type 'List<Widget>' How do I use a List<Dynamic> in a listview widget?

I am trying to use a list in a Listview widget in flutter but I keep getting an error saying that I need a List[Widget]. All the answers online make use of maps and I am still a beginner to Flutter. Can anyone show me how to use the map function for this or a way to convert List [Dynamic] to List [Widget]?
Here is my code:
import 'package:flutter/material.dart';
class NextPage extends StatefulWidget {
final List value;
NextPage({Key key, this.value}) : super(key: key);
#override
_NextPageState createState() => new _NextPageState();
}
class _NextPageState extends State<NextPage> {
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Reminders"),
),
body: ListView(
children: widget.value,
),
);
}
}
According to the discussion, value is List<String> in that case, to pass it on ListView we need to use these data and convert them into widget.
Here we are using value and making Text widget with it
import 'package:flutter/material.dart';
class NextPage extends StatefulWidget {
final List<String> value;
NextPage({Key key, this.value}) : super(key: key);
#override
_NextPageState createState() => new _NextPageState();
}
class _NextPageState extends State<NextPage> {
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Reminders"),
),
body: ListView(
children: widget.value.map((e) => Text(e)).toList(),
),
);
}
}
Does it solve the issue?

Not able to load local JSON File in Flutter

I am working on a personal Flutter Project which contains a few locally stored JSON Files
This is the code
class CCategory extends StatefulWidget {
#override
_CCategory createState() => _CCategory();
}
class Prod {
String Name;
String Image;
Prod({ this.Name, this.Image});
factory Prod.fromJson(Map<String, dynamic> parsedJson) {
return Prod(
Name: parsedJson['Name'],
Image: parsedJson['Image']);
}
}
Future<String> _loadProdAsset() async {
return await rootBundle.loadString('assets/data/Dabur.json');
}
Future<Prod> loadProd() async {
String jsonString = await _loadProdAsset();
final jsonResponse = json.decode(jsonString);
return new Prod.fromJson(jsonResponse);
}
class _CCategory extends State<CCategory> {
Prod _prod;
bool _loaded = false;
#override
void initState() {
super.initState();
loadProd().then((s) => setState(() {
_prod = s;
_loaded = true;
}));
}
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitDown,
DeviceOrientation.portraitUp,
]);
return MaterialApp(
title: "Dabur Products",
theme: ThemeData(
primaryColor: Colors.black,
),
home: Scaffold(
appBar: AppBar(
title: Text("Dabur Products",
),
),
body: _loaded?Center(
child: ListView(
children: <Widget>[
ListTile(
leading: Image.asset('${_prod.Image}'),
title: Text('${_prod.Name}'),
)
]
)
)
: new Center(
child: new CircularProgressIndicator(),
)
),
);
}
}
The contents of JSON file are not being loaded and this is the error I am encountering in debug
[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: type 'List' is not a subtype of type 'Map<String, dynamic>'
Can someone please help me resolve this ?
I don't know how your JSON-file looks like, but looking at your error code, json.decode(jsonString) seem to be giving you a List instead of a Map. I'd guess your JSON-file actually is a list:
[
... content ...
]
Instead, your JSON-file should look something like this (using { }):
{
"Name": ...,
"Image": ...
}

How to send and receive arguments between views in flutter

I'm trying to send data from a page to another in flutter project, tried all methods I found in another question but all failed, here is my code :
The first page :
Navigator.pushNamed(context, '/line_details', arguments: {'line':line,});
The second page:
class _LineDetailsState extends State<LineDetails> {
Map data = {};
#override
Widget build(BuildContext context) {
data = ModalRoute.of(context).settings.arguments;
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.deepPurple,
title: Text("$data"),
),
);
}
}
note: the line is a custom object created.
the error : always returns null, even when tried to send a single string as {"test": "test string"} it returns a null too
your Example is working fine
route in MaterialApp
routes: { "/line_details": (context) => LineDetails(), },
Push on FlatButton:
onPressed: () => Navigator.pushNamed(context, '/line_details', arguments: {'line':'test',}),
class LineDetails
class LineDetails extends StatefulWidget {
#override
_LineDetailsState createState() => _LineDetailsState();
}
class _LineDetailsState extends State<LineDetails> {
Map data = {};
#override
Widget build(BuildContext context) {
data = ModalRoute.of(context).settings.arguments;
print(data);
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.deepPurple,
title: Text("$data"),
),
);
}
}
with GestureDetector is also working
new GestureDetector(
onTap: () => Navigator.pushNamed(context, '/line_details', arguments: {'line':'test',}),
child: new Container(child: new Text("GestureDetector"),),
),
try this:
Navigator.push(
context,
LineDetailsState(
builder: (context) => LineDetailsState(
line: line
)))
and
class LineDetailsState extends StatefulWidget {
LineDetailsState(this.line);
Line line;
#override
_LineDetailsState createState() => _LineDetailsState();
}
class _LineDetailsState extends State<LineDetails> {
Map data = {};
#override
Widget build(BuildContext context) {
data = ModalRoute.of(context).settings.arguments;
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.deepPurple,
title: Text("$data"),
),
);
}
}
instead of "Line" replace it with name of your custom object
Your example looks proper one other thing you can try is typecast your argument
like below which help dart for linting. Note this is not related that you are getting null
class LineDetails extends StatefulWidget {
#override
_LineDetailsState createState() => _LineDetailsState();
}
class _LineDetailsState extends State<LineDetails> {
Map data = {};
#override
Widget build(BuildContext context) {
data = ModalRoute.of(context).settings.arguments as Map<String,object>;
print(data);
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.deepPurple,
title: Text("$data"),
),
);
}
}

Flutter pass json data to new Stateful Widget

I pass json data to new stateful widget in flutter. I can see data is not null in the debug console but stateful widget receive null data.
I tried other passing data type but still doesn't work.
How can I fix this? Any Help
Here is the code
Navigator Code
void goToFilteredList() async {
jsonData =await postQuotationFilteredList(
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
false,
false,
null,
null);
print(jsonData);
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => FilteredQuotationList(jsonData)));
}
Stateful Widget
class FilteredQuotationList extends StatefulWidget {
final List<Map<String, dynamic>> jsonData;
FilteredQuotationList(this.jsonData);
static const String routeName = "/filteredquotationlist";
#override
State<StatefulWidget> createState() =>
FilteredQuotationListScreen(this.jsonData);
}
jsonData has same type and there is the cons and this code received null data.
class FilteredQuotationListScreen extends State<FilteredQuotationList> {
List<Map<String, dynamic>> jsonData;
FilteredQuotationListScreen(List<Map<String, dynamic>> jsonData) {
this.jsonData = jsonData;
}
#override
Widget build(BuildContext context) {
print("filtreleme ekranı");
print(jsonData);
return Scaffold(
body: quotationFilteredListItems(jsonData),
);
}
ListView quotationFilteredListItems(List<Map<String, dynamic>> jsonData) {
print("quotation filtered list items");
List<GetQuotationModel> getQuotationModel =
GetQuotationModel.fromJson(jsonData);
print(getQuotationModel.length);
return ListView.builder(
itemCount: 3,
itemBuilder: (BuildContext context, int position) {
return Card(
color: Colors.amberAccent,
elevation: 2.0,
child: ListTile(
leading: CircleAvatar(
backgroundColor: Colors.green,
child: Text("getQuotationModel.quoteNumber"),
),
title: Text("this."),
),
);
},
);
}
}
First of all, this isn't how you should be using stateful widgets that are provided data via their constructor. Basically, the State object can access any parameter of the Widget object for free.
Here is an example of how it should be used.
class MyWidget extends StatefulWidget {
MyWidget({
Key key,
#required this.myParameter,
}) : super(key: key);
final String myParameter;
#override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
#override
Widget build(BuildContext context) {
return Text(widget.myParameter); // the widget accessor is included in every State
}
}
And then using it would look like this:
#override
Widget build(BuildContext context) {
return MyWidget(myParameter: "Hurray!");
}
Let me know if this fixes the issues you're having.

Proper structure with a Flutter App with JSON

I'm designing an app with a List-List-Detail view (an overview of lists, each list, details of the selected item from each list) that pulls all of its data from static JSON files.
I'm wondering what's the best structure for reading, parsing, and displaying the JSON files. Should I design models for each view: the overview of all lists, each list, and each item and have those as separate files? Should all of the asset loading and JSON parsing take place in those model files? Should the each view only accept parsed data in the form of a PODO?
I've been following the instructions here for the overall design, but I'm stuck on the best way to work with the JSON files, since they add an async element to Widget design.
Yes you need to create the Model class to handle the JSON response,
Either you can use the same model class to list-list, but dont forget to add child list items to same model
here is the sample example
import 'dart:async';
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
Future<List<Photo>> fetchPhotos(http.Client client) async {
final response =
await client.get('https://jsonplaceholder.typicode.com/photos');
// Use the compute function to run parsePhotos in a separate isolate
return compute(parsePhotos, response.body);
}
// A function that will convert a response body into a List<Photo>
List<Photo> parsePhotos(String responseBody) {
final parsed = json.decode(responseBody).cast<Map<String, dynamic>>();
return parsed.map<Photo>((json) => new Photo.fromJson(json)).toList();
}
class Photo {
final int albumId;
final int id;
final String title;
final String url;
final String thumbnailUrl;
Photo({this.albumId, this.id, this.title, this.url, this.thumbnailUrl});
factory Photo.fromJson(Map<String, dynamic> json) {
return new Photo(
albumId: json['albumId'] as int,
id: json['id'] as int,
title: json['title'] as String,
url: json['url'] as String,
thumbnailUrl: json['thumbnailUrl'] as String,
);
}
}
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
final appTitle = 'Isolate Demo';
return new MaterialApp(
title: appTitle,
home: new MyHomePage(title: appTitle),
);
}
}
class MyHomePage extends StatelessWidget {
final String title;
MyHomePage({Key key, this.title}) : super(key: key);
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(title),
),
body: new FutureBuilder<List<Photo>>(
future: fetchPhotos(new http.Client()),
builder: (context, snapshot) {
if (snapshot.hasError) print(snapshot.error);
return snapshot.hasData
? new PhotosList(photos: snapshot.data)
: new Center(child: new CircularProgressIndicator());
},
),
);
}
}
class PhotosList extends StatelessWidget {
final List<Photo> photos;
PhotosList({Key key, this.photos}) : super(key: key);
#override
Widget build(BuildContext context) {
return new GridView.builder(
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
),
itemCount: photos.length,
itemBuilder: (context, index) {
return new Image.network(photos[index].thumbnailUrl);
},
);
}
}

Categories

Resources