0

I am new to Flutter. I seen many tutorials on YouTube and Google but I can't find solution on this. How can I resolve this error?

import 'package:flutter/material.dart'; import 'dart:ui'; import 'dart:convert'; import 'data/shelfscreendata.dart'; import 'package:google_fonts/google_fonts.dart'; import 'constant/constant.dart' as Constants; import 'PhotographerWelcomeNote.dart'; import 'package:http/http.dart' as http; import 'Utility/global.dart' as Globals; import 'data/errordata.dart'; import 'data/shelfscreendata.dart'; import 'data/shelfscreendata.dart'; class ShelfScreen extends StatefulWidget { @override _ShelfScreenState createState() => _ShelfScreenState(); } class _ShelfScreenState extends State<ShelfScreen> { bool _isLoading = false; Map CompaignShelfList; List CampiagnData; Future Shelf() async { Map data = { "AccessToken": "MjUyLTg1REEyUzMtQURTUzVELUVJNUI0QTIyMTE=", "CustomerId": 1 }; final http.Response response = await http.post( "http://api.pgapp.in/v1/shelflist", headers: <String, String>{ 'Content-Type': 'application/json; charset=UTF-8', }, body: jsonEncode(data), ); var jsonResponse = null; CompaignShelfList = json.decode(response.body); setState(() { CampiagnData = CompaignShelfList["CompaignShelfList"]; }); // debugPrint(CompaignShelfList.toString()); if (response.statusCode == 200) { jsonResponse = json.decode(response.body); print("Response status : ${response.statusCode}"); print("Response status : ${response.body}"); if(jsonResponse != null && ! jsonResponse.containsKey("Error")){ setState(() { _isLoading = false; }); } else{ setState(() { _isLoading = false; }); print("Response status : ${response.body}"); } } } @override void initState() { super.initState(); Shelf(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( backgroundColor: const Color(0xfff8f8f8), leading: Padding( padding: EdgeInsets.only(left: 20), child: Image.asset("assets/logo.png",width: 45,height: 45,), ), title: Center( child: Image.asset("assets/Textimage.png",width: 170,height: 45), ), actions: <Widget>[ Padding( padding: const EdgeInsets.only(right: 20,top: 10,bottom: 10), child: Container( height: 8, width: 35, child: CircleAvatar( maxRadius: 20, backgroundImage: AssetImage("assets/images/prof.jpg"), ), ), ), ], ), body: GridView.builder( shrinkWrap: false, scrollDirection: Axis.vertical, gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: 1, childAspectRatio: 1.200, mainAxisSpacing: 1.0, crossAxisSpacing: 1.0, ), itemCount: CompaignShelfList.length, itemBuilder: (context, int index){ return CategoriesTile( imgUrls: CompaignShelfList[index]["CoverImagePath"], catagory:CompaignShelfList[index]["CampaignTitle"], date: CompaignShelfList[index]["Date"], email: CompaignShelfList[index]["PhotographerEmail"], stduio:CompaignShelfList[index]["BusinessName"], ); // Image.asset(Shelflist[index]["text_3"],fit: BoxFit.cover,); } ) ); } } class CategoriesTile extends StatelessWidget { final String imgUrls, catagory , date,email,stduio; CategoriesTile({@required this.imgUrls, @required this.catagory,@required this.date,@required this.email,@required this.stduio}); @override Widget build(BuildContext context) { return GestureDetector( onTap: () { Navigator.push( context, MaterialPageRoute( builder: (context) => PhotographerWelcomeNote( Coverpic: imgUrls, ))); }, child: Card( semanticContainer: true, clipBehavior: Clip.antiAliasWithSaveLayer, elevation: 5, child: Container( margin: EdgeInsets.all(8), child: Stack( children: <Widget>[ Positioned( top: 1, left: 1, right: 1, bottom: 50, child: ClipRRect( borderRadius: BorderRadius.circular(8), child:Image.asset( imgUrls, height: 250, width: 400, fit: BoxFit.cover, ) ), ), Positioned( bottom: 50, left: 1, right: 1, // width: 400, height: 60, child: ClipRect( child: BackdropFilter( filter: ImageFilter.blur(sigmaX: 3, sigmaY: 3), child: Container( decoration: BoxDecoration( color: Colors.black.withOpacity(0), borderRadius: BorderRadius.circular(8), ), ), ), ), ), Positioned( bottom: 80, left: 20, child: Container( height: 50, width: 400, alignment: Alignment.bottomLeft, child: Text( catagory ?? "Yo Yo", style: GoogleFonts.nunito( fontSize: 18, color: const Color(0xffffffff), fontWeight: FontWeight.w700, height: 1.7647058823529411, ), )), ), Positioned( left: 20, bottom: 60, child: Container( height: 30, width: 150, alignment: Alignment.bottomLeft, child: Text( date,style: GoogleFonts.nunito( fontSize: 13, color: const Color(0xffffffff), fontWeight: FontWeight.w700, height: 1.7647058823529411, ), ), ), ), Positioned( bottom: 20, left: 90, child: Container( height: 23, alignment: Alignment.center, child: Text( stduio, style: GoogleFonts.nunito( fontSize: 18, color: const Color(0xff7f7f7f), fontWeight: FontWeight.w700, ) ), ), ), Positioned( bottom: 1, left: 90, child: Container( height: 20, alignment: Alignment.center, child: Text( email,style: GoogleFonts.nunito( fontSize: 10, color: const Color(0xffb4b4b4), fontWeight: FontWeight.w600, ), ), ), ), Positioned( bottom: 2, left: 10, child: Container( width: 66.0, height: 39.0, decoration: BoxDecoration( image: DecorationImage( image: const AssetImage( 'assets/images/camlogo.jpg'), fit: BoxFit.cover, ), border: Border.all( width: 1.0, color: const Color(0xffb4b4b4)), ), ), ), ], ), ), ), ); } } 
4
  • debugging is looking at the console with error, looking at the stack trace, and the 99% of the time error will tell you on which line on which character the error is being thrown and what exactly is wrong, and it will tell you what you should do, and it'll be easier for us to. Commented Nov 14, 2020 at 6:43
  • Hi there, I've two suggestions. 1. Can you confirm that you are actually getting some data from the API call? It would be really helpful if you could provide the error message with stack. 2. You should NEVER put your code as is with sensitive data, such as AccessToken here. Just mask it with * or put a comment or anything other than the actual token value :) Commented Nov 14, 2020 at 6:54
  • Your API seems broken. Id don't get answer from that. This can be issue that your response body can't convert to a Json. Commented Nov 14, 2020 at 12:19
  • Looks like someone is having the same error here - same homework? Commented Nov 15, 2020 at 11:07

1 Answer 1

0

The error x was called on null is usually thrown if the variable where the method was called hasn't been initialized. You may want to look into CompaignShelfList = json.decode(response.body); and see if it there's a response that can decoded before fetching the value from the Map with a key using CompaignShelfList["CompaignShelfList"]

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.