为 JSON 制作 Pojo 类

问题描述

有人可以帮我设计 pojo 类以跟随 json 吗

     "t_details":{
              // this numeric value is dynamic value from server
        "980303030303": {  
      "key1": "27389237482744","key2": ""
         }
         }`

我需要为上面提到的 JSON 制作响应类(Getter Setter),其中 KEY 是动态值

解决方法

为此,您可以为 JSON 对象的每一对创建 HasMap。如下图

    class ProfileClient extends StatefulWidget {
      const ProfileClient({Key key}) : super(key: key);
    
      @override
      _ProfileClientState createState() => _ProfileClientState();
    }
    
    class _ProfileClientState extends State<ProfileClient> {
      final FirebaseAuth auth = FirebaseAuth.instance;
    
      String name ;
      String email ;
      String biographie ;
      String nationalite ;
      String genre ;
      String niveau ;
      String specialite ;
      String equipment ;
      String langue ;
      String age ;
      String rs;
    
      Future getData; //Add this line

   
      //Add this code block
      @override
      void initState() {
        super.initState();

        getData =
          FirebaseFirestore.instance
                .collection('users')
                .doc(auth.currentUser.uid)
                .get()
                .then((DocumentSnapshot documentSnapshot) {
              if (documentSnapshot.exists) {
                print('Document data: ${documentSnapshot.data()}');
                setState(() {
                  name = (documentSnapshot.data() as Map)['name'];
                  email = (documentSnapshot.data() as Map)['email'];
                  biographie = (documentSnapshot.data() as Map)['biographie'];
                  nationalite = (documentSnapshot.data() as Map)['nationality'];
                  genre = (documentSnapshot.data() as Map)['genre'];
                  niveau = (documentSnapshot.data() as Map)['niveau'];
                  specialite = (documentSnapshot.data() as Map)['specialite'];
                  equipment = (documentSnapshot.data() as Map)['equipment'];
                  langue = (documentSnapshot.data() as Map)['langue'];
                  age = (documentSnapshot.data() as Map)['age'];
                  rs = (documentSnapshot.data() as Map)['rs'];
                });
              }else {
                print('Document not found');
                return null;
              }
            }
            );
      }

      TextStyle style1() {
        return TextStyle(
            color: Colors.blue.shade900,fontSize: 15,fontWeight: FontWeight.w900);
      }
    
      TextStyle style2() {
        return TextStyle(color: Colors.blue.shade900,fontSize: 15);
      }
      @override
      Widget build(BuildContext context) {
        //MainScreen is just a class that return appbar bottom bar navigation and one background image
        return MainScreen(
          currentIndex: 3,isSelectedHome: false,isSelectedSecond: false,isSelectedFourth: false,isSelectedThird: true,child: new FutureBuilder(
              future: getData,//Update this line
              builder: (BuildContext context,AsyncSnapshot<dynamic>snapshot) {
    
                switch(snapshot.connectionState) {
                  case ConnectionState.none:
                    return Scaffold(body: Center(child: Text('Check your internet connection!!')));
                  case ConnectionState.active:
                  case ConnectionState.done:
                    return new ListView(
                      children: [
                        //this is how i show the data
                        Padding(
                          padding: const EdgeInsets.fromLTRB(25,30,25,10),child: Container(
                            color: Colors.white70,child: new Row(
                              children: [
                                new Text(
                                  'nom:',style: style1(),),Expanded(
                                    child: Text(
                                      name,style: style2(),)),],);
                  default: return ListView(
                    children: [
                      Same ListView...
                    ],);
                }
              }
          )
        );
      }
    }

你的内部数据模型

class YourModel {

      Map<String,DataModel> data = new HashMap <String,DataModel>();

      public void setValue(Map<String,DataModel> map)
       {
         this.data = map;
       }
      public Map<String,DataModel> getValue()
       {
        return this.data;
       }
    }