0% found this document useful (0 votes)
34 views6 pages

Api

This document contains code for two Dart classes that make API calls to retrieve dictionary data. The Cricketpage class loads cricket data from a local JSON file using the FutureBuilder widget. The SearchPage class defines a stateful widget that allows users to search for word definitions. It makes API requests to the OwlBot dictionary API and displays the results in a StreamBuilder.

Uploaded by

zubair7123315
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views6 pages

Api

This document contains code for two Dart classes that make API calls to retrieve dictionary data. The Cricketpage class loads cricket data from a local JSON file using the FutureBuilder widget. The SearchPage class defines a stateful widget that allows users to search for word definitions. It makes API requests to the OwlBot dictionary API and displays the results in a StreamBuilder.

Uploaded by

zubair7123315
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

// import 'dart:convert';

// import 'dart:html';
// import 'dart:html';

import 'dart:convert';
//https://www.fiverr.com/share/VBRL4B

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_zubair_dictionary/Model_work/cricket_model.dart';
import 'package:http/http.dart';
// import 'package:http/http.dart';

// class GetApi extends StatefulWidget {


// @override
// _GetApiState createState() => _GetApiState();
// }

// class _GetApiState extends State<GetApi> {


// var jsonPost = [];

// final url = "https://jsonplaceholder.typicode.com/posts";


// final url = "https://api.dictionaryapi.dev/api/v2/entries/en/hello";
// // final url = "https://api.dictionaryapi.dev/api/v2/entries/en/<word>";
// void getData() async {
// final response = await get(Uri.parse(url));
// final decodeData = jsonDecode(response.body);
// setState(() {
// jsonPost = decodeData;
// });
// }

// @override
// void initState() {
// super.initState();
// getData();
// }

// @override
// Widget build(BuildContext context) {
// return Scaffold(
// body: ListView.builder(
// itemCount: jsonPost.length,
// itemBuilder: (c, i) {
// final post = jsonPost[i];
// return Text("Title:${post['title']}\n");
// // Body:${post["body"]}
// }),
// );
// }
// }

// class GetApi extends StatefulWidget {


// @override
// _GetApiState createState() => _GetApiState();
// }

// class _GetApiState extends State<GetApi> {


// var jsonpost;
// var jsonDecode;
// void loadAsset() async {
// final loadJson = await rootBundle
// .loadString("https://api.dictionaryapi.dev/api/v2/entries/en/hello");
// final jsonDecode = await json.decode(loadJson);
// setState(() {
// jsonpost = jsonDecode;
// });
// print(jsonpost['hello']);
// }

// @override
// void initState() {
// loadAsset();
// super.initState();
// }

// @override
// Widget build(BuildContext context) {
// return Scaffold(body: ListView.builder(itemBuilder: (c, i) {
// final post = jsonpost[i];
// return Text("Body:${post["hello"]};
// }));
// }
// }
class Cricketpage extends StatelessWidget {
Future<cricketModel> loadcricketdata() async {
var loadjson = await rootBundle.loadString('json_data/cricket_json.json');
var jsondecode = await json.decode(loadjson);
var c = cricketModel.myjson(jsondecode);
return c;
}

@override
Widget build(BuildContext context) {
loadcricketdata();
return Scaffold(
body: FutureBuilder(
future: loadcricketdata(),
builder: (c, AsyncSnapshot<cricketModel> snapshot) {
if (!snapshot.hasData) {
return CircularProgressIndicator();
}
if (snapshot.connectionState == ConnectionState.waiting) {
return CircularProgressIndicator();
}
return Center(
child: Card(
color: Colors.amber,
child: Text('${snapshot.data!.crickformet}'),
),
);
}),
);
}
}
2ND CODE

import 'dart:async';
import 'dart:convert';
import 'dart:ffi';
import 'package:http/http.dart';
import 'package:flutter/material.dart';
import 'package:flutter_zubair_dictionary/TabBarView/Dictionary.dart';
import 'package:flutter_zubair_dictionary/TabBarView/Thesaurus.dart';

class SearchPage extends StatefulWidget {


static const id = '/SearchPage';

@override
_SearchPageState createState() => _SearchPageState();
}

class _SearchPageState extends State<SearchPage> {


String url = " https://owlbot.info/api/v4/dictionary/";
String token = "fb205c1d10156395e1bc1de2a7652b75c73622e3";
TextEditingController controllar = TextEditingController();
late final StreamController streamController;
late final Stream stream;
search() async {
if (controllar.text == null || controllar.text.length == 0) {
streamController.add(null);
}
Response response = await get(Uri.parse(url + controllar.text.trim()),
headers: {"Authorization": "Token " + token});
// var jsonResponse = response.body;
streamController.add(json.decode(response.body));
print(response.body);
// print('fidsjfiashfaijs');
}

@override
void initState() {
super.initState();
streamController = StreamController();
stream = streamController.stream;
}

@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 2,
child: Scaffold(
appBar: AppBar(
bottom: TabBar(tabs: [
Padding(
padding: const EdgeInsets.only(bottom: 10),
child: Text(
'Dictionary',
style: TextStyle(fontSize: 20),
),
),
Padding(
padding: const EdgeInsets.only(bottom: 10),
child: Text(
'Thesaurus',
style: TextStyle(fontSize: 20),
),
),
]),
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Padding(
padding: const EdgeInsets.only(right: 5),
child: Expanded(
flex: 10,
child: Container(
width: 225,
height: 45,
child: TextFormField(
controller: controllar,
onChanged: (String text) {},
style: TextStyle(color: Color(0xff265667)),
decoration: InputDecoration(
prefixIcon: Padding(
padding: const EdgeInsets.only(top: 2),
child: IconButton(
color: Color(0xff265667),
onPressed: () {
search();
setState(() {
controllar.selection;
});
},
icon: Icon(
Icons.search,
size: 32,
// color: Colors.blue,
),
),
),
border: InputBorder.none,
hintText: 'Search for a word',
hintStyle: TextStyle(fontSize: 15),
fillColor: Colors.black26,
suffixIcon: Padding(
padding: const EdgeInsets.only(bottom: 10),
child: IconButton(
onPressed: () {
search();
setState(() {
controllar.clear();
});
},
icon: Icon(Icons.close),
color: Color(0xff265667),
iconSize: 35,
),
)),
),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(7))),
),
),
Expanded(
flex: 2,
child: Container(
child: IconButton(
splashRadius: Material.defaultSplashRadius,
onPressed: () {
setState(() {});
},
icon: Icon(
Icons.mic,
size: 40,
))),
)
],
),
),
body: StreamBuilder(
stream: stream,
builder: (BuildContext cts, AsyncSnapshot snapshot) {
// if (snapshot.data == null) {
// Center(child: Text("Enter Search Word"));
// }
return ListView.builder(
itemCount: snapshot.data["definitions"],
itemBuilder: (BuildContext context, int index) {
return ListBody(
children: <Widget>[
Container(
color: Colors.grey,
child: ListTile(
leading: snapshot.data["definitions"][index]
["image_url"] ==
null
? null
: CircleAvatar(
backgroundImage: NetworkImage(
snapshot.data["definitions"][index]
["image_url"]),
),
title: Text(controllar.text.trim() +
"(" +
snapshot.data["definitions"][index]["type"] +
")"),
),
)
],
);
});
},
)
// TabBarView(children: [
// Dictionary(),
// Thesaurus(),
// ]
// ),
),
);
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy