I'm trying to display the Laravel Api response data without ListView for profile page where I'm getting the api response but not able to display the data without ListView.builder in flutter and even not able to define the response in setState() method about .
I have fetched all the user profile details from api.
My code looks like:
class ProfilePage extends StatefulWidget {
final String scratchCard;
const ProfilePage({Key key, this.scratchCard}) : super(key: key);
@override
_ProfilePageState createState() => _ProfilePageState( scratchCard);
}
class _ProfilePageState extends State<ProfilePage> {
// Map profile = jsonDecode(result.body);
// List<Map<String, dynamic>> profile;
bool isLoading = false;
String student_name;
bool _visible = false;
String scratchCard;
String text;
String user;
String email;
String institute;
String mobile;
String referral_code;
String reg_no;
String name;
String first_name;
String last_name;
String subject_name;
String standard_name;
String icon;
var institute_mobile;
var institute_icon;
var subject;
var standard;
var student;
bool premium;
_ProfilePageState(this.scratchCard);
@override
void initState() {
super.initState();
this.getProfileDetails();
}
@override
void dispose() {
super.dispose();
}
getProfileDetails() async {
setState(() {
isLoading = true;
});
var response = await CallApi().getProfile();
print(response.body);
if(response.statusCode == 200){
var items = json.decode(response.body)['user'];
setState(() {
// institute = items.student[0].institute.name;
student_name = items['user']['first_name'] + ' ' + items['user']['last_name'];
institute = items['student'][0]['institute']['name'];
email = items['user']['email'];
mobile = items['user']['mobile'];
referral_code = items['user']['referral_code'];
reg_no = items['student'][0]['reg_no'];
institute_mobile = items['student'][0]['institute']['user']['mobile'];
institute_icon = items['student'][0]['institute']['icon'];
subject=items.subject_name;
this.standard=items['subject_name']['standard_name'];
isLoading = false;
});
}else{
// tableData = [];
isLoading = false;
}
}
@override
Widget build(BuildContext context) {
var subject;
return Scaffold(
extendBodyBehindAppBar: true,
body: Stack(
children: <Widget>[
Container(
height: 275,
decoration: BoxDecoration(
borderRadius: BorderRadius.vertical(bottom: Radius.circular(30.0)),
color: Constants.blueLight,
),
child: Stack(
children: <Widget>[
Positioned(
child: Image.asset("assets/images/blob_2.png",
width: 332,
height: 323,
color: Constants.blueDark),
top: -80,
right: -110,
),
Positioned(
child: Image.asset("assets/images/blob_1.png",
width: 255,
height: 267,
color: Constants.blueMain),
top: 20,
left: -80,
),
],
),
),
// HeaderInner(),
ListView(
scrollDirection: Axis.vertical,
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
children: <Widget>[
Container(
// color: Colors.blue[600],
// height: 40 * SizeConfig.heightMultiplier,
height: 100 ,
child: Padding(
padding: EdgeInsets.only(left: 20.0, right: 20.0, top: 0 ),
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Container(
height:60, width: 60,
// height: 11 * SizeConfig.heightMultiplier,
// width: 22 * SizeConfig.widthMultiplier,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
fit: BoxFit.cover,
image: NetworkImage('https://github.com/slackvishal/flutter_traveler_profile_app/blob/master/assets/profileimg.png?raw=true'))
),
),
SizedBox(width: 10,),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(student_name.toString(), style: TextStyle(
color: Colors.white,
// fontSize: 3 * SizeConfig.textMultiplier,
fontWeight: FontWeight.bold, fontSize: 16
),),
Text(email.toString(), style: TextStyle(
color: Colors.white70,fontSize: 13
// fontSize: 1.9 * SizeConfig.textMultiplier,
),),
],
),
],
),
// SizedBox(height: ),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.white60),
borderRadius: BorderRadius.circular(5.0),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text("EDIT PROFILE", style: TextStyle(
color: Colors.white60,
),),
),
),
],
),
],
),
),
),
Container(
height: 550,
padding: EdgeInsets.fromLTRB(
Constants.mainPadding,
Constants.mainPadding * 1.5,
Constants.mainPadding,
Constants.mainPadding
),
decoration: BoxDecoration(
borderRadius: BorderRadius.vertical(top: Radius.circular(50.0)),
color: Colors.white,
),
child:ListView(
scrollDirection: Axis.vertical,
shrinkWrap: true,
children: [
InkWell(
onTap: () {
Navigator.push(
context, MaterialPageRoute(builder: (context) => Standard()));
},
child: Container(
width: double.infinity,
padding: EdgeInsets.only(left:20.0, right: 20, top: 13, bottom: 13),
margin: EdgeInsets.only(bottom: 10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30.0),
color: Constants.lightPink,
),
child: Row(
children: <Widget>[
Icon(Icons.location_city, color: Constants.textDark, size: 20,),
// Image.asset("assets/images/education-logo.jpg", width: 40, height: 40),
SizedBox(width: 30.0),
Expanded(
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 0),
child:Text( 'Institute : ' ,
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.w500,
color: Constants.textDark
),
),
),
SizedBox(width: 5,),
Container(
margin: EdgeInsets.only(top: 2),
child: Text(institute.toString(),
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
color: Constants.textDark
),
),
),
],
),
),
],
),
),
),
InkWell(
onTap: () {
// Navigator.push(
// context, MaterialPageRoute(builder: (context) => Standard()));
},
child: Container(
width: double.infinity,
padding: EdgeInsets.only(left:20.0, right: 20, top: 13, bottom: 13),
margin: EdgeInsets.only(bottom: 10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30.0),
color: Constants.lightYellow,
),
child: Row(
children: <Widget>[
Icon(Icons.phone, color: Constants.textDark, size: 20,),
// Image.asset("assets/images/education-logo.jpg", width: 40, height: 40),
SizedBox(width: 30.0),
Expanded(
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 0),
child:Text( "Mobile: ",
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.w500,
color: Constants.textDark
),
),
),
SizedBox(width: 5,),
Container(
margin: EdgeInsets.only(top: 2),
child: Text(mobile.toString(),
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
color: Constants.textDark
),
),
),
SizedBox(height: 5,),
],
),
),
],
),
)
),
InkWell(
onTap: () {
Navigator.push(
context, MaterialPageRoute(builder: (context) => Standard()));
},
child: Container(
width: double.infinity,
padding: EdgeInsets.only(left:20.0, right: 20, top: 13, bottom: 13),
margin: EdgeInsets.only(bottom: 10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30.0),
color: Constants.lightBlue,
),
child: Row(
children: <Widget>[
Icon(Icons.code, color: Constants.textDark, size: 20,),
// Image.asset("assets/images/education-logo.jpg", width: 40, height: 40),
SizedBox(width: 30.0),
Expanded(
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 0),
child:Text( "Referral Code: ",
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.w500,
color: Constants.textDark
),
),
),
SizedBox(width: 5,),
Container(
margin: EdgeInsets.only(top: 2),
child: Text(referral_code.toString(),
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
color: Constants.textDark
),
),
),
],
),
),
],
),
)
),
InkWell(
onTap: () {
Navigator.push(
context, MaterialPageRoute(builder: (context) => Standard()));
},
child: Container(
width: double.infinity,
padding: EdgeInsets.only(left:20.0, right: 20, top: 13, bottom: 13),
margin: EdgeInsets.only(bottom: 10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30.0),
color: Constants.lightViolet,
),
child: Row(
children: <Widget>[
Icon(Icons.confirmation_number, color: Constants.textDark, size: 20,),
// Image.asset("assets/images/education-logo.jpg", width: 40, height: 40),
SizedBox(width: 30.0),
Expanded(
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 0),
child:Text( "Registration No.: ",
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.w500,
color: Constants.textDark
),
),
),
SizedBox(width: 5,),
Container(
margin: EdgeInsets.only(top: 2),
child: Text(reg_no.toString(),
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
color: Constants.textDark
),
),),
],
),
),
],
),
)
),
InkWell(
onTap: () {
Navigator.push(
context, MaterialPageRoute(builder: (context) => Standard()));
},
child: Container(
width: double.infinity,
padding: EdgeInsets.only(left:20.0, right: 20, top: 13, bottom: 13),
margin: EdgeInsets.only(bottom: 10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30.0),
color: Constants.lightLime,
),
child: Row(
children: <Widget>[
Icon(Icons.report, color: Constants.textDark),
// Image.asset("assets/images/education-logo.jpg", width: 40, height: 40),
SizedBox(width: 30.0),
Expanded(
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 0),
child:Text( "Referral Percentage: ",
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.w500,
color: Constants.textDark
),
),
),
SizedBox(width: 5,),
Container(
margin: EdgeInsets.only(top: 2),
child: Text("5%",
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
color: Constants.textDark
),
),
),
],
),
),
],
),
)
),
],
),
),
])
]),
);
}
}
I'm getting this error below:
E/flutter ( 8258): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: NoSuchMethodError: The method '[]' was called on null.
E/flutter ( 8258): Receiver: null
E/flutter ( 8258): Tried calling: []("first_name")
E/flutter ( 8258): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
E/flutter ( 8258): #1 _ProfilePageState.getProfileDetails.<anonymous closure> (package:spotway_app/screens/profile.dart:100:36)
E/flutter ( 8258): #2 State.setState (package:flutter/src/widgets/framework.dart:1089:30)
E/flutter ( 8258): #3 _ProfilePageState.getProfileDetails (package:spotway_app/screens/profile.dart:97:7)
E/flutter ( 8258): <asynchronous suspension>