Home > Enterprise >  For loop Expected an identifier.Expected to find ')' error
For loop Expected an identifier.Expected to find ')' error

Time:02-18

I am using for loop in my flutter project and I got an error ithat says Expected an identifier.dart(missing_identifier) Expected to find ')'.

Error

Error

Here's my code

HomeView File

import 'package:flutter/material.dart';
import 'package:puzzlegame/Widgets/customBox.dart';
import 'dart:io';

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

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

class _HomeViewState extends State<HomeView> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Puzzle Game'),
      ),
      body: Center(child: 
      Container(
        child: 
      for (int i=0; i<4; i  ){
        for (int j=0; j<4; j  ){
          Box(),
        }
        
      },  
        
        ),
      
      ),
      
    );
  }
}

CodePudding user response:

I see a some problems with your code:

  1. Container expect only one child.
  2. Your "for" doesn't return a widget, and Container is expecting a widget as a child..

I'm not sure what are you trying to do but if you want to have more than one Box you should use a Widget that supports more than one child (like column, listview, row, wrap, etc). Also, don't use "for" in widget tree.

Also, check the documentation about multichild widgets https://docs.flutter.dev/development/ui/widgets/layout

What are you trying to implement? maybe if you share a screen of the intended UI we could help more.

  • Related