Home > Back-end >  How to solve The argument type 'JsObject' can't be assigned to the parameter type �
How to solve The argument type 'JsObject' can't be assigned to the parameter type �

Time:11-14

I want to call a class and it needs a parameter. I have tried to insert 'context' on it, but instead importing 'context' from BuildContext, it imports context from dart:js. So I don't know what I should insert to the parameter.

class NewAreaItem extends StatelessWidget {
  const NewAreaItem({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 1000,
      width: 1280,
      padding: const EdgeInsets.all(5),
      child: GridView.builder(
        shrinkWrap: true,
        itemCount: 9,
        gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
          maxCrossAxisExtent: 450,
          mainAxisExtent: 250,
          crossAxisSpacing: 50,
          mainAxisSpacing: 30,
        ),
        itemBuilder: (context, index) => buildArea(index),
      ),
    );
  }

  Widget buildArea(int index) => Card(
        color: const Color.fromARGB(255, 209, 209, 209),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(15),
        ),
        child: Container(
          padding: const EdgeInsets.all(8),
          child: buildAreaItem(),
        ),
      );

  Widget buildAreaItem() {
    return Padding(
      padding: const EdgeInsets.only(left: 15, bottom: 25, top: 25, right: 15),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              Container(
                padding: const EdgeInsets.all(1),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(5),
                  border: Border.all(color: Colors.black, width: 1),
                ),
                child: IconButton(
                  icon: const Icon(Icons.more_horiz),
                  iconSize: 15,
                  onPressed: () {
                    UpdateDeleteAreaItem.updateDeleteDialog(context); // <--- Context that error
                  },
                ),
              ),

This is the class which I try to call. It's just a class, neither using StatelessWidget nor StatefulWidget. Because this class is called inside a button.

class UpdateDeleteAreaItem {
  static Future<Action> updateDeleteDialog(
    BuildContext context,
  ) async {
    final action = await showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(10),
            ),

CodePudding user response:

Pass context through method constructor:

Widget buildAreaItem(BuildContext context){ //<--- add this
  return ...
}

then change your buildArea tot this:

Widget buildArea(int index, BuildContext context) => Card(
    color: const Color.fromARGB(255, 209, 209, 209),
    shape: RoundedRectangleBorder(
      borderRadius: BorderRadius.circular(15),
    ),
    child: Container(
      padding: const EdgeInsets.all(8),
      child: buildAreaItem(context),
    ),
  );

and also change your itemBuilder to this:

itemBuilder: (context, index) => buildArea(index, context),
  • Related