0
import 'dart:io';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:campus/Service/Firebase_Firestore_END.dart';

class Profile extends StatefulWidget {
  File _imagefile;

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

class _ProfileState extends State<Profile> {
  String url;


**Use This Method To get URL From Firestore**
  
void getAvatar() async {
    final FirebaseAuth auth = FirebaseAuth.instance;
    final FirebaseUser user = await auth.currentUser();
    final uid = user.uid;
    DocumentSnapshot data = await Firebase_Firestore_END.getAccData(uid) as DocumentSnapshot;
    url= data.data['Avatar'];
    print("URL:"+url);
    // return data.data['Avatar'];
  }

  @override
  void initState(){
    getAvatar(); //Call Methor Here
    super.initState();

  }



  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Stack(
          children: <Widget>[
            Container(
              decoration: BoxDecoration(
                  border:
                  Border.all(width: 1, color: Colors.blueGrey),
                  borderRadius:
                  BorderRadius.all(Radius.circular(50))),
              child: CircleAvatar(
                radius: 50,
                backgroundColor: Colors.transparent,
                backgroundImage: widget._imagefile != null
                    ? FileImage(widget._imagefile)
                    **: CachedNetworkImageProvider(url),//Faliled Assertion Here**
              ),
            ),
          ],
        ),
      ),
    );
  }
}

What is the problem with this code? In Hot Reload It Works but If I restart The App. The Error (url=null) occurred. Can anyone help me to find the error? I tried in so many ways but I face the same error. I don't know why it's happened. I call url setter function in

initState() but every times url was null during start

3 Answers 3

1

Change this code:

  String url;

To

  String url = '';

and also can change this one, because async function returns Future<void>

Future<void> getAvatar() async{
...
}
Sign up to request clarification or add additional context in comments.

Comments

1

First of all you need to initialize url with

String url = ''

But I think the problem is, that the getAvatar() function is async. This causes the code to finish without waiting for the function. Unfortunately you can not await inside initState. Wrap the part where you set url in a setState(); like this:

setState((){url = ...});

Additionally you could use something like a placeholder until the image or url has been loaded.

1 Comment

why you edit your answer after my answer?
0

You may have used a single quotation mark instead of "to define and request the url for example:

 setState(() {
      var productJson= json.decode(utf8.decode(response.bodyBytes));
      for(var i in productJson){
        var productIteam=product(i['product_name'], i['id'],i['price'] , i["image_url"], i['off'], i['description']);
         _items.add(productIteam);
      }
    });

this code true because use "" => i["image_url"] but this code not true i['image_url']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.