Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

android - How to fix the error type 'String' is not a subtype of type 'int' of 'index'?

I am new to flutter and after following StackOverflow guidances I managed to find how to access specific parts of the values in the json tree and print those values of specific keys in a map. But now I'm getting the error type 'String' is not a subtype of type 'int' of 'index'

This is my code.

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CupertinoApp(
      localizationsDelegates: <LocalizationsDelegate<dynamic>>[
        DefaultMaterialLocalizations.delegate,
        DefaultWidgetsLocalizations.delegate,
        DefaultCupertinoLocalizations.delegate,
      ],
      home: TransactionHistoryScreen(),
    );
  }
}

class TransactionHistoryScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      child: Scaffold(body: Container(child: MakeList(),),),
    );
  }
}

class MakeList extends StatelessWidget {
final List<Map<String, String>> json = [
  {"branch": "B1", "xyz": "0", "ABC": "2", "MN": "2"},
  {"branch": "B2", "xyz": "0", "ABC": "0", "MN": "0"},
];

@override
Widget build(BuildContext context) {
  return ListView.builder(
    itemCount: json.length,
    itemBuilder: (BuildContext context, int index) {
      return ListTileClass(jsonObject: json[index]);
    },
   );
  }
}

class ListTileClass extends StatefulWidget {
final Map<String, String> jsonObject;

ListTileClass({this.jsonObject});

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

class _ListTileClassState extends State<ListTileClass> {
  @override
  Widget build(BuildContext context) {
    return ListTile(
      title: Container(
        child: Row(
          children: makeWidgetChildren(widget.jsonObject),
        ),
      ),
    );
  }

  List<Widget> makeWidgetChildren(jsonObject) {
    List<Widget> children = [];
    jsonObject.keys.forEach(
      (key) => {
        children.add(
          Padding(
              child: Text('${key['branch']}'), padding: EdgeInsets.all(8.0)),
        ),
      },
    );
    return children;
  }
}

Could I please know how to fix this error? Thank you!

question from:https://stackoverflow.com/questions/65626367/how-to-fix-the-error-type-string-is-not-a-subtype-of-type-int-of-index

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are iterating over jsonObject.keys:

var keys = ["branch", "xyz", "ABC", "MN"] 
keys.forEach((key) => print('${key['branch']}')); # A value of type 'String' can't be assigned to a variable of type 'int'.

So you are trying to iterate over Strings.

To solve t:

Text('${jsonObject[key]}')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...