Skip to main content

Random Meal generator App in Flutter

In this article, we will use mealDB api to make http requests, and get the data we want.

What data we are going to get from this api - 

  • Recipe Name
  • Recipe Type (Dessert or Lunch)
  • Dish Image
  • Instructions to make the Dish 


Now, follow the below steps to complete this project. 


1. Add http as dependency inside pubspec.yaml file otherwise you will get errors.

2. Create a file named constants.dart and add following code to it - 

import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; class Constants{ static const regularHeading = TextStyle(color: Colors.black, fontWeight: FontWeight.600,fontSize:30); }


3. Create one more file named api.dart, where we will be storing our api and then import it in main.dart to make http requests.

Write this code inside api.dart - 

                       import 'package:flutter/material.dart';

                        String url = 'api here';

Take the api from mealDB and assign it to url.

4. Now, inside main.dart -

import 'dart:convert'; import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:random_meal_generator/api.dart';
import 'package:random_meal_generator/constants.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Map mapResponse;
List meals;
Future getMeal() async {
http.Response response;
response =
await http.get(url);
if (response.statusCode == 200) {
setState(() {
mapResponse = json.decode(response.body);
meals = mapResponse['meals'];
});
}
}
@override
void initState() {
getMeal();
super.initState();
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Todays Recipe", style: TextStyle(color: Colors.white,fontSize: 20), textAlign: TextAlign.center,
),
backgroundColor: Colors.black,),
body: meals == null
? Text("wait")
: ListView.builder(
itemCount: meals == null ? 0 : meals.length,
itemBuilder: (context, index) {
return Container(
padding: EdgeInsets.all(20),
child: Column(
children: <Widget>[
SizedBox(height: 20,),
Text(meals[index]['strCategory'], style: Constants.regularHeading,),
Text(meals[index]['strMeal'], style: TextStyle(color: Colors.black,fontSize: 40, fontWeight: FontWeight.w700),),
Image.network(meals[index]['strMealThumb']),
SizedBox(height: 20,),
Text("Recipe",style: Constants.regularHeading,),
SizedBox(height: 10,),
Text(meals[index]['strInstructions'], style: TextStyle(fontSize: 20),),
],
),
);
},
));
}
}

This will be the output - 







Everytime you open the app you will get different random recipe. I hope you understood how we are doing it, if you have any doubt, do ask in comments or contact us.

Comments

Popular posts from this blog

List of Questions asked by Morgan Stanley

  Coding Questions -  1.  .   Exchange kth node from start and kth node from end of a singly linked list. 2.    Given a number, you have to find next greater number which has same set of digits. 3.    Given a binary tree, you have to print level order traversal of the tree (left child then right child) but every next level has to be printed in next line. 4.    Longest Common Substring 5.    Spiral level order traversal 6.    Add two linked lists 7.    Write a function to find the mirror image of binary tree 8.    WAP to find the character which occurred maximum times in the character array 9.  Given a 2d matrix find an element in a matrix which is 0 and make the entire row and column to 0 10.  Find the minimum element in the rotated array of integers 11.  Find highest length substring such that there are equal number of 0’s and 1’sin array of 1’s and 0’s only 12.  Given a dictio...

Which is best: LeetCode vs HackerEarth ?

  Well, all the platforms almost are same except the interface. That's what I think. To conclude the result, let's compare them through the features they offer.  Features of Leetcode -  Topic - wise questions Comapny-wise questions Weekly contests Interview Preparation section Discuss section Features of HackerEarth Topic-wise problems Monthly contests Discuss section Hiring Contests Hackathons Earlier, when I started coding in my first year. I was fond of HackerEarth. But when I stoped using hackerearth and shifted to Leetcode, I like it more. It depends upon how much comfortable you are with the interface. But, it can't be denied that Leetcode offer more than HackerEarth. But for the coders who love hackathons, HackerEarth is best for them and for the people looking out for job opportunities. Do share your favorite through comments!

Top Questions asked by Facebook

                  Facebook is the dream company of almost every software engineer. If you are doing interview preparation, you can't skip the must do coding questions for Facebook. Facebook is the product-based company and tests the candidate on data structures and algorithms, and logical questions. The coding level is a bit tough, but you can do it with your hard work. Below is the list of coding questions you must prepare before sitting for Facebook. Smallest string which not a subsequence of the given string Maximum length possible by cutting N given woods into at least K pieces Count possible decodings of a given Digit Sequence | Set 2 Find the maximum path sum in a binary tree   Maximize profit for buying and selling stock given a series of price vectors Given  m  sorted arrays, find the  k- th smallest value Binary search in a sorted, rotated array Given a string, check if it is a palindrome  by ignoring spaces....