Deepak
Deepak

Reputation: 2188

flutter error: _InternalLinkedHashMap<String, dynamic>

Error-> Class '_InternalLinkedHashMap<String, dynamic>' has no instance getter 'psubCatName'. Receiver: _LinkedHashMap len:9 Tried calling: psubCatName

This is my TabPage I'm Trying to fetch data inside tab of TabBar using map.

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:hospital/customApiVariable.dart';
import 'package:http/http.dart' as http;

class TabPage extends StatefulWidget {
  final medicineCatUniqId;
  const TabPage({Key key, this.medicineCatUniqId}) : super(key: key);

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

class _TabPageState extends State<TabPage> {
  var response;


  var medicineSubCategoryApi;

  @override
  void initState() {
    // TODO: implement initState
    //
    super.initState();
// for loading
    fetchData(widget.medicineCatUniqId);
  }

  fetchData(var medicineCatUniqId) async {
    var api = Uri.parse('$baseUrl/productSubCatApi.php?a2rTokenKey=$a2rTokenKey&pcat=$medicineCatUniqId');
    response = await http.get(
      api,
    );
    print("medicineCatApiLnk " + api.toString());
    print("medicineCat" + response.body);
    medicineSubCategoryApi = jsonDecode(response.body);

    print("medicineCatString" + medicineSubCategoryApi.toString());
    return medicineSubCategoryApi;
    // setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: MaterialApp(
        home: ListView(
          children: [
            FutureBuilder(
                future: fetchData(medicineSubCategoryApi),
                // initialData: medicineSubCategoryApi,
                builder: (context, snapshot) {
                  if (snapshot.hasData) {

                    print(snapshot.data.length.toString());
                    print(snapshot.data.toString());
                    // return Column(
                    //   crossAxisAlignment: CrossAxisAlignment.center,
                    //   children: [
                    //     Text(snapshot.data.length.toString()),
                    //     Text(snapshot.data.toString()),
                    //   ],
                    // );

                    // API data will be stored as snapshot.data
                    return DefaultTabController(
                      length: snapshot.data.length,
                      child: Scaffold(
                        appBar: AppBar(
                          title: const Text('Tabbed AppBar'),
                          bottom: TabBar(
                            isScrollable: true,
                            tabs: snapshot.data
                                .map((choice) => Tab(
                                      text: choice.psubCatName,
                                      // icon: Icon(choice),
                                    ))
                                .toList(),
                          ),
                        ),
                        body: TabBarView(
                          children: snapshot.data.map<Widget>((choice) {
                            return Padding(
                              padding: const EdgeInsets.all(20.0),
                              child: ChoicePage(
                                choice: choice,
                              ),
                            );
                          }).toList(),
                        ),
                      ),
                    );
                  } else if (snapshot.hasError) {
                    return Text('Error');
                  } else {
                    return Text('Loading');
                  }
                }),
          ],
        ),
      ),
    );
  }
}

class ChoicePage extends StatelessWidget {
  const ChoicePage({Key key, this.choice}) : super(key: key);
  final choice;

  @override
  Widget build(BuildContext context) {
    final TextStyle textStyle = Theme.of(context).textTheme.display1;
    return Card(
      color: Colors.white,
      child: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            
            Text(
              choice.psubCatName,
              style: textStyle,
            ),
          ],
        ),
      ),
    );
  }
}

But when I'm using This thing then my data is showing .

if (snapshot.hasData) {
 return Column( 
crossAxisAlignment: CrossAxisAlignment.center, 
children: [
 Text(snapshot.data.length.toString()),
 Text(snapshot.data.toString()),
 ],
 );
} 

but when I am using This thing then show error Inside tab of TabBar. This is Error ->'Class _InternalLinkedHashMap<String, dynamic>' has no instance getter 'psubCatName'. Receiver: _LinkedHashMap len:9 Tried calling: psubCatName

                   if (snapshot.hasData) {
                    print(snapshot.data.length.toString());
                    print(snapshot.data.toString());
                    // API data will be stored as snapshot.data
                    return DefaultTabController(
                      length: snapshot.data.length,
                      child: Scaffold(
                        appBar: AppBar(
                          title: const Text('Tabbed AppBar'),
                          bottom: TabBar(
                            isScrollable: true,
                            tabs: snapshot.data
                                .map((choice) => Tab(
                                      text: choice.psubCatName,
                                      // icon: Icon(choice),
                                    ))
                                .toList(),
                          ),
                        ),

Upvotes: 0

Views: 514

Answers (1)

Sajad Abdollahi
Sajad Abdollahi

Reputation: 1741

The choice object that you used is a map and to access map values instead of

choice.psubCatName

You should use:

choice['psubCatName']

Anyway it's a good practice to use FutureBuilder with Data Type Like this:

FutureBuilder<YoureDataType>()

And in this case

FutureBuilder<Map>()

Upvotes: 1

Related Questions