Repository: flutter
Issue: Unexpected result with DateUtils.addDaysToDate
Labels: framework,f: material design,has reproducible steps,P2,found in release: 3.7,found in release: 3.10,team-design,triaged-design

### Is there an existing issue for this?

- [X] I have searched the [existing issues](https://github.com/flutter/flutter/issues)
- [X] I have read the [guide to filing a bug](https://flutter.dev/docs/resources/bug-reports)

### Steps to reproduce

I have found occasionally DateUtils passes different results depending on whether you use `=` or a function. e.g.
```
final int maxPlanLength = 24;

/// If the date is 24/04/23 - the following results in lastDate = 2nd Oct. This is incorrect.
final lastDate = DateUtils(DateTime.now().dateOnly, (maxPlanLength - 1) * 7 + 1).dateOnly );

/// However, this produces the correct result (3rd Oct).
DateTime lastPlanDate() => DateUtils.addDaysToDate(DateTime.now().dateOnly, (maxPlanLength - 1) * 7 + 1).dateOnly;

```

Is this a bug? 

### Expected results

```
/// However, this produces the correct result (3rd Oct).
DateTime lastPlanDate() => DateUtils.addDaysToDate(DateTime.now().dateOnly, (maxPlanLength - 1) * 7 + 1).dateOnly;
```

### Actual results

```
/// If the date is 24/04/23 - the following results in lastDate = 2nd Oct. This is incorrect.
final lastDate = DateUtils.addDaysToDate(DateTime.now().dateOnly, (maxPlanLength - 1) * 7 + 1).dateOnly );
```

### Code sample

<details><summary>Code sample</summary>

```dart
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

extension DateTimeX on DateTime {
  DateTime get dateOnly => DateUtils.dateOnly(this);
}

class _MyHomePageState extends State<MyHomePage> {
  // int _counter = 0;

  // void _incrementCounter() {
  //   setState(() {
  //     // This call to setState tells the Flutter framework that something has
  //     // changed in this State, which causes it to rerun the build method below
  //     // so that the display can reflect the updated values. If we changed
  //     // _counter without calling setState(), then the build method would not be
  //     // called again, and so nothing would appear to happen.
  //     _counter++;
  //   });
  // }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.

    const maxPlanLength = 24;
    final lastDateText = DateUtils.addDaysToDate(DateTime.now().dateOnly, (maxPlanLength - 1) * 7 + 1).dateOnly;
    DateTime lastPlanDate() => DateUtils.addDaysToDate(DateTime.now().dateOnly, (maxPlanLength - 1) * 7 + 1).dateOnly;

    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          // Column is also a layout widget. It takes a list of children and
          // arranges them vertically. By default, it sizes itself to fit its
          // children horizontally, and tries to be as tall as its parent.
          //
          // Invoke "debug painting" (press "p" in the console, choose the
          // "Toggle Debug Paint" action from the Flutter Inspector in Android
          // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
          // to see the wireframe for each widget.
          //
          // Column has various properties to control how it sizes itself and
          // how it positions its children. Here we use mainAxisAlignment to
          // center the children vertically; the main axis here is the vertical
          // axis because Columns are vertical (the cross axis would be
          // horizontal).
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text("lastDateText ${lastDateText.toIso8601String()}"),
            Text("lastDateFunction ${lastPlanDate().toIso8601String()}")
          ],
        ),
      ),
      // floatingActionButton: FloatingActionButton(
      //   onPressed: _incrementCounter,
      //   tooltip: 'Increment',
      //   child: const Icon(Icons.add),
      // ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

```

</details>


### Screenshots or Video

<details>
<summary>Screenshots / Video demonstration</summary>

[Upload media here]

</details>


### Logs

<details><summary>Logs</summary>

```console
[Paste your logs here]
```

</details>


### Flutter Doctor output

<details><summary>Doctor output</summary>

```console
[✓] Flutter (Channel stable, 3.7.12, on macOS 13.3.1 22E261 darwin-arm64, locale en-GB)
    • Flutter version 3.7.12 on channel stable at /Develop/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 4d9e56e694 (7 days ago), 2023-04-17 21:47:46 -0400
    • Engine revision 1a65d409c7
    • Dart version 2.19.6
    • DevTools version 2.20.1

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
    • Android SDK at /Library/Android/sdk
    • Platform android-33, build-tools 33.0.2
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14E222b
    • CocoaPods version 1.12.1

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)

[✓] VS Code (version 1.77.3)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.62.0

[✓] Connected device (3 available)
    • iPhone (91) (mobile) • 00008110-000918AE14D0401E • ios            • iOS 16.4.1 20E252
    • macOS (desktop)      • macos                     • darwin-arm64   • macOS 13.3.1 22E261 darwin-arm64
    • Chrome (web)         • chrome                    • web-javascript • Google Chrome 112.0.5615.137
    ! Error: Apple Watch needs to connect to determine its availability. Check the connection between the device and its companion iPhone, and
      the connection between the iPhone and Xcode. Both devices may also need to be restarted and unlocked. (code 1)

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
```

</details>