0

I have an app with two stateful pages: ListPage and InsertPage. When the user double clicks on an item in the ListPage, the InsertPage shall open and allow him to edit the data record he clicked on.

Here are relevant code fragments:

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

  @override
  _ListPageState createState() {
    return _ListPageState();
  }
}


class _ListPageState extends State<ListPage> {
// .....    
    // The RecordAdmin class provides a static field in which all objects of 'MyRecord' are stored
    List<MyRecord> recordList = RecordAdmin.overallRecordList;
// .....    
    onDoubleTap: () {
                
                // Causing a compiler error, see blow
                Navigator.of(context).push(MaterialPageRoute(
                    builder: (context) => InsertPage(
                          constructorRecord: recordList[index],
                        )));


// .....

class InsertPage extends StatefulWidget {
  
  final MyRecord? constructorRecord;

  const InsertPage({Key? ipkey, this.constructorRecord}) : super(key: ipkey);

  @override
  _InsertPageState createState() {
    return _InsertPageState();
  }
}
  
class _InsertPageState extends State<InsertPage> {
  
  final _formKey = GlobalKey<FormState>();

  // How to trigger this function from class _ListPageState???
  void editRecord(MyRecord receivedRecord) {
    setState(() {
      myControllerItem.text = receivedRecord.item;
// ...    
      

I already did some research on how to pass data between classes (here implemented by an constructor parameter - which sadly doesn't work, see compiler error below) and how to trigger a function from another widget (here editRecord() ) - but the results don't really fit to my situation.

As mentioned, the current code isn’t working currently as the compiler stops with the following error since I implemented the passing of a MyRecord-Object to the constructor of the class InsertPage:

/// Does nothing if asserts are disabled. Always returns true.
bool debugCheckHasMaterial(BuildContext context) {
  assert(() {
    if (LookupBoundary.findAncestorWidgetOfExactType<Material>(context) == null) {
      final bool hiddenByBoundary = LookupBoundary.debugIsHidingAncestorWidgetOfExactType<Material>(context);
      throw FlutterError.fromParts(<DiagnosticsNode>[
        ErrorSummary('No Material widget found${hiddenByBoundary ? ' within the closest LookupBoundary' : ''}.'),
        if (hiddenByBoundary)
          ErrorDescription(
            'There is an ancestor Material widget, but it is hidden by a LookupBoundary.'
          ),
        ErrorDescription(
          '${context.widget.runtimeType} widgets require a Material '
          'widget ancestor within the closest LookupBoundary.\n'
          'In Material Design, most widgets are conceptually "printed" on '
          "a sheet of material. In Flutter's material library, that "
          'material is represented by the Material widget. It is the '
          'Material widget that renders ink splashes, for instance. '
          'Because of this, many material library widgets require that '
          'there be a Material widget in the tree above them.',
        ),
        ErrorHint(
          'To introduce a Material widget, you can either directly '
          'include one, or use a widget that contains Material itself, '
          'such as a Card, Dialog, Drawer, or Scaffold.',
        ),

Can anyone help?

Rasputin221
  • 51
  • 1
  • 3
  • Update: When i place the constructor parameter for myrecord into the class of the NavigationRail, there is no compiler error any more. From the NavigationRail class, when calling another page, i have to pass the received "myrecord" parameter to this page constructor and then its accessible there too. I would be interested if there is a simpler way instead of passing the parameter through 2 constructors... – Rasputin221 Jun 14 '23 at 19:13

0 Answers0