0

I'm trying to create settings page from json. But first i'm checking if creating is working on static array:

import 'package:flutter/material.dart';
import 'package:settings_ui/settings_ui.dart';

class _SettingsScreenState extends State<SettingsScreen> {
   bool lockInBackground = true;

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

@override
Widget build(BuildContext context) {
    var tilearray = [{'name':'settings no 1'},{'name':'settings no 2'},{'name':'settings no 3'}];
    List<SettingsTile> tiles = [];
    log('{$tilearray}');
    tiles.add( SettingsTile.switchTile(
        initialValue: lockInBackground,
        onToggle: (bool value) {
          setState(() {
            lockInBackground = value;
          });
        },
        leading: Icon(Icons.phonelink_lock),
        title: Text('settings no 0')));
    tilearray.map((item) => log('test{$item.name}'));
    tilearray.map((item) => tiles.add( SettingsTile.switchTile(
        initialValue: lockInBackground,
        onToggle: (bool value) {
          setState(() {
            lockInBackground = value;
          });
        },
        leading: Icon(Icons.phonelink_lock),
        //title: Text(item['name']!))));
        title: Text('debug item'))));
   log('{$tiles}');
   return Scaffold(
      appBar: AppBar(title: Text('Settings UI')),
      body: SettingsList(
        sections: [
          SettingsSection(
            title: Text('First Section'),
            tiles: tiles,
          )
        ]
      )
    );
  }
}

But the result is only one SettingsTile (Setting no 0)...

The comment I add to check if there's a problem with array item, but no. It looks like tilearray is empty.

It is strange that tilearray.map((item) => log('test{$item.name}')); is empty too

3
  • Current format will break the context chain Commented Oct 22, 2022 at 20:53
  • Do not understand... Commented Oct 22, 2022 at 20:54
  • the ui update because because you are putting everything inside build method, also everything is going to reassign . I will give an within 5min Commented Oct 22, 2022 at 20:57

2 Answers 2

1

Follow current way.

try putting variable outside the build method or you can use initState


class SettingsScreen extends StatefulWidget {
  const SettingsScreen({super.key});

  @override
  State<SettingsScreen> createState() => _SettingsScreenState();
}

class _SettingsScreenState extends State<SettingsScreen> {
  @override
  void initState() {
    super.initState();
  }

  Map<String, bool> tilearray = {
    'settings no 1': false,
    'settings no 2': false,
    'settings no 3': false
  };

  List<SettingsTile> getTiles(BuildContext context) {
    List<SettingsTile> tiles = [];

    for (int i = 0; i < tilearray.length; i++) {
      final question = tilearray.keys.elementAt(i);
      final value = tilearray.values.elementAt(i);
      tiles.add(SettingsTile.switchTile(
          initialValue: value,
          onToggle: (bool value) {
            setState(() {
              tilearray[question] = value;
            });
          },
          leading: Icon(Icons.phonelink_lock),
          title: Text(question)));
    }
    ;
    return tiles;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Settings UI')),
      body: SettingsList(
        sections: [
          SettingsSection(
            title: Text('First Section'),
            tiles: getTiles(context),
          )
        ],
      ),
    );
  }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Nope. I put it initState (and move List<SettingsTile> tiles = []; to Class) - but still the same - map doesn't work
Can you provide full but minimal widget that will reproduce the same error?
edited. But I put tilearray.map((item) => log('x{$item.name}')); and no log - it looks like tilearray is empty
The issue is with key
do the way you like but on your structure you are already having key value for single map, cases like this i prefer using helper/adapter class to simplify the process
|
0

Got it!

I have to use

 for (final item in tilearray){
      log('x{$item}');
      tiles.add( SettingsTile.switchTile(
        initialValue: lockInBackground,
        onToggle: (bool value) {
          setState(() {
            lockInBackground = value;
          });
        },
        leading: Icon(Icons.phonelink_lock),
        //title: Text(item['name']!))));
        title: Text('element')));
      log('{$tiles}');
    }
  }

instead of

tilearray.map((item) => 

And as @Yeasin Sheikh wrote - have to move it to initState()

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.