获取用户数据 API 不工作 flutter 集成

get user data API not working flutter integration

下图显示了我 API 获取用户数据的过程。它工作成功。现在我希望它连接到我的 flutter 代码以在应用栏上显示个人资料图片和用户名。我创建了 getdata() 方法来连接 API。但它不工作。我该如何更正并显示在应用栏上。

class Dashboard extends StatefulWidget {
  const Dashboard({Key? key}) : super(key: key);

  @override
  State<Dashboard> createState() => _DashboardState();
}

class _DashboardState extends State<Dashboard> {
  bool dashoard = true;
  bool isLoading = true;

  String profilePicture = '';
  String username = '';

  @override
  void initState() {
    super.initState();
    loading();

  }

  loading() async {
    await getdata();

  }

  getdata() async {
    var response;
    try {
      response = await Dio()
          .get(BASE_API + "user/getUserById/" + loginUserData["id"]!);

      print("response: $response");
      Map<String, dynamic> responseJson = json.decode(response.toString());

      setState(() {
        profilePicture = responseJson['data']['image'];
        username = responseJson['data']['userName'];
      });
      // print(responseJson['data']['userName']);

    } catch (e) {
      print(e);

    }
  }

Widget appbar() {
    return Padding(
      padding: const EdgeInsets.only(top: 10),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
            
           
              Image.asset(
                "assets/images/userimage.png",
                scale: 1,
              ),
           

            ],
          ),

           Row(
             children: [
              Text(
                username,
                style: TextStyle(
                    color: Colors.white, fontSize:12),
              ),
             ],
           )
        ],
      ),
    );
  }

试试这个

列表<地图<字符串,动态>>? responseJson;

可变响应;

try {
          response = await Dio().get(BASE_API + "user/getUserById/" +loginUserData["id"]!);

          print("response: $response");
              response = json.decode(response.toString());


     for(var items in response){
          responseJson!.add(items);
        }


          setState(() {
            //profilePicture = responseJson['data']['image'];
            //username = responseJson['data']['userName'];
            profilePic = responseJson[0]["image"]; // new line added
          });
          // print(responseJson['data']['userName']);

        } catch (e) {
          print(e);

        }